pytroll / satpy

Python package for earth-observing satellite data processing
http://satpy.readthedocs.org/en/latest/
GNU General Public License v3.0
1.06k stars 291 forks source link

Combining RGB and RGBA color palettes in enhancements #1658

Open KziolkowskiWx opened 3 years ago

KziolkowskiWx commented 3 years ago

When trying to combine color palettes in the enhancements that are of two types (RGB, and RGBA) an error is produced due to the difference in size between the arrays. The RGB array has 3 columns and RGBA has 4, so due to the size different the arrays cannot be concatenated. Evening using colormap_mode: "RGBA" to force the RGB array to become 4 columns does not seem to do the trick.

Here is an example of using a Satpy built in color palette (spectral) with a min/max range, and adding a second color palette with two transparent black values with a min/max value.

  ir_clouds_1:
    standard_name: ir_clouds_1
    operations:
      - name: colorize
        method: *colorizefun 
        kwargs:
          palettes:
            - {colormap_mode: "RGBA", colors: spectral, min_value: 205.15, max_value: 245.149999}
            - {colormap_mode: "RGBA", colors: [[0, 0, 0, 0], [0, 0, 0, 0]], min_value: 245.15, max_value: 303.15}

[DEBUG: 2021-05-03 23:03:49 : satpy.writers] Enhancement configuration options: [{'name': 'colorize', 'method': <function colorize at 0x7f236d96d710>, 'kwargs': {'palettes': [{'colormap_mode': 'RGBA', 'colors': 'spectral', 'min_value': 205.15, 'max_value': 245.149999}, {'colormap_mode': 'RGBA', 'colors': [[0, 0, 0, 0], [0, 0, 0, 0]], 'min_value': 245.15, 'max_value': 303.15}]}}]
Traceback (most recent call last):
  File "/home/ziolkowskik/satdev/scripts/meso_east1_split_window_ir.py", line 111, in <module>
    main()
  File "/home/ziolkowskik/satdev/scripts/meso_east1_split_window_ir.py", line 39, in main
    scn.load(imagery)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/scene.py", line 975, in load
    keepables = self.generate_composites()
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/scene.py", line 888, in generate_composites
    return self._read_composites(nodes)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/scene.py", line 863, in _read_composites
    self._generate_composite(item, keepables)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/scene.py", line 838, in _generate_composite
    **self.attrs)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/composites/__init__.py", line 1588, in __call__
    foreground = enhance2dataset(projectables[0])
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/composites/__init__.py", line 1133, in enhance2dataset
    img = get_enhanced_image(dset)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/writers/__init__.py", line 466, in get_enhanced_image
    enhancer.apply(img, **dataset.attrs)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/writers/__init__.py", line 1057, in apply
    fun(img, *args, **kwargs)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/enhancements/__init__.py", line 197, in colorize
    full_cmap = _merge_colormaps(kwargs)
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/satpy/enhancements/__init__.py", line 221, in _merge_colormaps
    full_cmap = full_cmap + cmap
  File "/home/ziolkowskik/anaconda3/envs/satdev_test/lib/python3.7/site-packages/trollimage/colormap.py", line 116, in __add__
    new.colors = np.concatenate((self.colors, other.colors))
  File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 4

What should occur is the built-in Satpy color palette should be forced to a 4 column array as an RGBA with the alpha value set to 1 so the color palette can be used on a range of values and can be combined with a transparent or another color palette that has the color mode of RGBA.

Environment Info:

djhoese commented 3 years ago

This should be possible with the current main branch of trollimage, but still needs to be released. I'll try to remember to do that next week.