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

Add more flexible masking #1175

Merged
merged 21 commits into from May 25, 2020
Merged

Conversation

pnuu
Copy link
Member

@pnuu pnuu commented May 4, 2020

This PR adds a more flexible way to apply masking to composites.

@pnuu pnuu added enhancement code enhancements, features, improvements component:compositors labels May 4, 2020
@pnuu pnuu requested review from mraspaud and djhoese May 4, 2020 06:56
@pnuu pnuu self-assigned this May 4, 2020
@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

It seems that my initial idea of adding a modifier isn't feasible, as the modifier can't be used to add alpha channel. Thus I went with yet another compositor.

The first test image looks like this:
ir_test_20200417_060000

Here brightness temperature values above 270 K are fully transparent, and values from 260 to 270 are with 50 % transparency and the values are inverted before saving.

Corresponding composite definition:

  ir_test:
    compositor: !!python/name:satpy.composites.MaskCompositor
    conditions:
    # operator, limit/threshold/value, transparency
    # the below is equivalent to
    #    alpha_val = 1. - transparency / 100.
    #    data = data.where(np.greater_equal(mask, 260), alpha_val, 1.0)`
    - [greater_equal, 260, 50]
    - [greater_equal, 270, 100]
    prerequisites:
    - name: IR_108
    - name: IR_108
    standard_name: ir_test

and enhancement:

  ir_test:
    standard_name: ir_test
    operations:
    - name: invert
      method: *stretchfun
      kwargs:
        stretch: crude
        # Note: also the alpha-channel limits need to be defined!
        min_stretch: [300, 0]
        max_stretch: [215, 1]

@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

I'm not sure what causes the weird pattern on the right of the image. It's not visible when I open the original GeoTIFF image in GIMP, but it is visible when viewed with qiv.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 89.534% when pulling 6ba1aa6 on pnuu:feature-mask-compositor into 65cd83f on pytroll:master.

@coveralls
Copy link

coveralls commented May 4, 2020

Coverage Status

Coverage increased (+0.2%) to 89.766% when pulling 0853863 on pnuu:feature-mask-compositor into 65cd83f on pytroll:master.

@codecov
Copy link

codecov bot commented May 4, 2020

Codecov Report

Merging #1175 into master will increase coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1175      +/-   ##
==========================================
+ Coverage   89.60%   89.67%   +0.07%     
==========================================
  Files         200      200              
  Lines       29487    29738     +251     
==========================================
+ Hits        26422    26669     +247     
- Misses       3065     3069       +4     
Impacted Files Coverage Δ
satpy/composites/__init__.py 79.53% <100.00%> (+0.76%) ⬆️
satpy/tests/test_composites.py 100.00% <100.00%> (ø)
satpy/readers/avhrr_l1b_gaclac.py 93.33% <0.00%> (-1.34%) ⬇️
satpy/readers/hrit_jma.py 97.94% <0.00%> (-1.22%) ⬇️
satpy/readers/yaml_reader.py 95.30% <0.00%> (-0.13%) ⬇️
satpy/writers/cf_writer.py 93.77% <0.00%> (-0.03%) ⬇️
satpy/writers/geotiff.py 90.74% <0.00%> (ø)
satpy/readers/netcdf_utils.py 100.00% <0.00%> (ø)
satpy/readers/seviri_l1b_native.py 71.96% <0.00%> (ø)
satpy/tests/reader_tests/test_ahi_hrit.py 100.00% <0.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 65cd83f...0853863. Read the comment docs.

@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

As a side note: if I save directly to PNG or omit the tiled=True kwarg to the lcl.save_datasets() the image is fine.

@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

Would it be more clear to have

    - method: greater_equal
      value: 260
      transparency: 50

instead of

    - [greater_equal, 260, 50]

?
Or maybe there are better names for the "dictionary" version above, or another completely different way to give these information. Suggestions are wellcome! I'm going to adapt for the method/value/transparency approach in the meanwhile, it seems much more clear.

@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

The composite configuration is now like this:

  ir_test:
    compositor: !!python/name:satpy.composites.MaskingCompositor
    conditions:
    # the below makes everything above 270 K completely transparent, and values between 250 ... 270 K 50 % transparent
    - method: greater_equal
      value: 260
      transparency: 50
    - method: greater_equal
      value: 270
      transparency: 100
    prerequisites:
    # this is the data that will be shown
    - name: IR_108
    # this is used for masking with conditions shown above
    - name: IR_108
    standard_name: ir_test

@pnuu
Copy link
Member Author

pnuu commented May 4, 2020

In the end I'll be merging this to the existing MaskingCompositor which was created to handle specifically masking with categorical cloud type data from NWC SAF PPS/GEO.

@pnuu pnuu marked this pull request as ready for review May 5, 2020 10:48
@pnuu pnuu requested a review from adybbroe as a code owner May 5, 2020 10:48
@pnuu
Copy link
Member Author

pnuu commented May 5, 2020

Now also the existing composites that were using MaskingCompositor are converted to use the new syntax, and the functionalities are merged in to one compositor.

@pnuu pnuu added this to To do in PCW Spring 2020 via automation May 11, 2020
@pnuu pnuu moved this from To do to Ready for review in PCW Spring 2020 May 11, 2020
@pnuu
Copy link
Member Author

pnuu commented May 13, 2020

I added one new composite that uses this feature: hrv_severe_storms_masked. It also uses a new feature, where np.isnan can be used to create a mask. Otherwise the data outside HRV channel would get a non-transparent background in the alpha channel.

@pnuu
Copy link
Member Author

pnuu commented May 13, 2020

crop_hrv_severe_storms_masked_20200417_060000

This is a crop of the new hrv_severe_storms_masked composite around central Africa. The composite is inted to be used overlayd on a static background, so it doesn't look very good on the white background.

@pnuu
Copy link
Member Author

pnuu commented May 25, 2020

I can't make Codebeat any happier. The MaskingCompositor.__init__() needs all the arguments, and the other complaints are to files that I didn't touch.

Copy link
Member

@mraspaud mraspaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one nitpick


"""
try:
func = getattr(np, method)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in principle this means any numpy method can be called. Just for security reasons, would you mind adding a check that method is not outside what we expect ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a list of accepted methods, and an unit test for trying to use something else. I hope I listed all the currently relevant methods.

PCW Spring 2020 automation moved this from Ready for review to Ready to merge May 25, 2020
@mraspaud mraspaud merged commit 836c657 into pytroll:master May 25, 2020
PCW Spring 2020 automation moved this from Ready to merge to Done May 25, 2020
@pnuu pnuu deleted the feature-mask-compositor branch May 25, 2020 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:compositors enhancement code enhancements, features, improvements
Projects
Development

Successfully merging this pull request may close these issues.

Add more versatile options for masking datasets
3 participants