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 an argument to the compositor to switch alpha band on/off in DayNightCompositor #2358

Merged
merged 19 commits into from
Jan 23, 2023

Conversation

yukaribbba
Copy link
Contributor

@yukaribbba yukaribbba commented Jan 19, 2023

This PR is a solution for #2357. It only takes effect in "day only" or "night only" mode.

@codecov
Copy link

codecov bot commented Jan 19, 2023

Codecov Report

Merging #2358 (5127851) into main (ff7f237) will increase coverage by 0.03%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2358      +/-   ##
==========================================
+ Coverage   94.60%   94.64%   +0.03%     
==========================================
  Files         318      324       +6     
  Lines       47719    48337     +618     
==========================================
+ Hits        45146    45747     +601     
- Misses       2573     2590      +17     
Flag Coverage Δ
behaviourtests 4.44% <2.12%> (-0.05%) ⬇️
unittests 95.27% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
satpy/composites/__init__.py 90.51% <100.00%> (+0.06%) ⬆️
satpy/tests/test_composites.py 100.00% <100.00%> (ø)
satpy/readers/netcdf_utils.py 90.25% <0.00%> (-8.07%) ⬇️
satpy/readers/viirs_sdr.py 84.54% <0.00%> (-2.40%) ⬇️
satpy/readers/yaml_reader.py 97.26% <0.00%> (-0.23%) ⬇️
satpy/readers/olci_nc.py 93.81% <0.00%> (-0.16%) ⬇️
satpy/tests/reader_tests/test_ghi_l1.py 100.00% <0.00%> (ø)
satpy/tests/reader_tests/test_nwcsaf_nc.py 100.00% <0.00%> (ø)
satpy/tests/reader_tests/test_fci_l1c_nc.py 100.00% <0.00%> (ø)
satpy/tests/reader_tests/test_viirs_atms_utils.py 100.00% <0.00%> (ø)
... and 16 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@coveralls
Copy link

coveralls commented Jan 19, 2023

Coverage Status

Coverage: 95.23% (+0.03%) from 95.204% when pulling 5127851 on yukaribbba:main into ff7f237 on pytroll:main.

@yukaribbba yukaribbba requested a review from pnuu as a code owner January 19, 2023 14:58
Copy link
Member

@djhoese djhoese left a comment

Choose a reason for hiding this comment

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

I have one inline change to say include_alpha instead of need_alpha, but otherwise I have one other question/suggestion:

What if you forced the other data to be NaN in this case instead of 0 (like it is now)? This would mean you'd have to specify fill_value=0 on your save_datasets call, but I think this is more inline with other composites that Satpy produces...maybe? Put another way, the alpha band would be added during save_datasets rather than in the compositor.

satpy/composites/__init__.py Outdated Show resolved Hide resolved
Co-authored-by: David Hoese <david.hoese@ssec.wisc.edu>
@yukaribbba
Copy link
Contributor Author

I have one inline change to say include_alpha instead of need_alpha, but otherwise I have one other question/suggestion:

What if you forced the other data to be NaN in this case instead of 0 (like it is now)? This would mean you'd have to specify fill_value=0 on your save_datasets call, but I think this is more inline with other composites that Satpy produces...maybe? Put another way, the alpha band would be added during save_datasets rather than in the compositor.

Yeah I tried that before but didn't work out so I turned to the compositor. It will force an alpha band added to the image and I think that's the reason. Only when it turned off the fill_value=0 has the effect.

@djhoese
Copy link
Member

djhoese commented Jan 19, 2023

Yeah I tried that before but didn't work out so I turned to the compositor. It will force an alpha band added to the image and I think that's the reason. Only when it turned off the fill_value=0 has the effect.

Sorry I wasn't clear. I understand that the existing way the compositor work wasn't working as it was always adding an alpha band. What I'm suggesting is that in this compositor it currently replaces the masked data with 0 making it black. I think when this alpha flag is False that this should instead be np.nan so that the result of the compositor is still a single band, but the unwanted data is NaN instead of 0. This, by default, will produce an alpha band in the geotiff writer, but if you specify fill_value=0 in Scene.save_datasets it should become black. Since the compositor isn't creating the Alpha band this should work.

@yukaribbba
Copy link
Contributor Author

Sorry I wasn't clear. I understand that the existing way the compositor work wasn't working as it was always adding an alpha band. What I'm suggesting is that in this compositor it currently replaces the masked data with 0 making it black. I think when this alpha flag is False that this should instead be np.nan so that the result of the compositor is still a single band, but the unwanted data is NaN instead of 0. This, by default, will produce an alpha band in the geotiff writer, but if you specify fill_value=0 in Scene.save_datasets it should become black. Since the compositor isn't creating the Alpha band this should work.

Sorry I got a little confused...

day_data = foreground_data if "day" in self.day_night else 0
night_data = foreground_data if "night" in self.day_night else 0

You're talking about these two lines? I could add another IF level for the False flag and make them np.nan. Is that correct? Not quite sure…

@djhoese
Copy link
Member

djhoese commented Jan 19, 2023

Yeah I think that would work. It might also work to just replace 0 with np.nan since in the Alpha case those would be masked/transparent anyway.

@yukaribbba
Copy link
Contributor Author

Yeah I think that would work. It might also work to just replace 0 with np.nan since in the Alpha case those would be masked/transparent anyway.

Got it. I’ll finish it tomorrow. Heading to bed now.😁

@yukaribbba
Copy link
Contributor Author

Yeah I think that would work. It might also work to just replace 0 with np.nan since in the Alpha case those would be masked/transparent anyway.

I tried replacing 0 with np.nan in these two lines. However I always got both night and day portions masked-out despite other arguments. So I had to send it back to 0. I had several tests under the latest commit and it worked perfectly for me. You can see the upper left one is just what I need.
image

@djhoese
Copy link
Member

djhoese commented Jan 20, 2023

Ah it is because the merging of the day and the night is a multiply and an addition:

data = night_portion + day_portion

Doing that with NaN is not going to work. Hhhmm I'm about to go to sleep. I'll have to think about this more.

@yukaribbba
Copy link
Contributor Author

yukaribbba commented Jan 20, 2023

What about this? We send np.nan to coszen instead of the data itself, keeping the data at 0, and we can also make data = night_portion + day_portion safe.

@yukaribbba
Copy link
Contributor Author

yukaribbba commented Jan 20, 2023

And the test results:
image

The last one is a little different from above. Since the night portion pixels are replaced with NaN, this area is considered as outter space. This is like what you said I suppose.

@yukaribbba yukaribbba requested review from djhoese and removed request for mraspaud and pnuu January 20, 2023 14:23
@yukaribbba
Copy link
Contributor Author

Whoops…

@djhoese djhoese added enhancement code enhancements, features, improvements component:compositors labels Jan 20, 2023
@djhoese
Copy link
Member

djhoese commented Jan 20, 2023

The difference you're talking about is how the bottom-right case doesn't "fade" into night time like the alpha=True cases, right? Yeah I think this makes sense. I'm not sure if it will be expected by all users but I think it is the only option if you don't want an alpha channel and also want only day(night) data.

@mraspaud @pnuu @gerritholl any opinions on this?

@yukaribbba
Copy link
Contributor Author

I'm not sure if it will be expected by all users but I think it is the only option if you don't want an alpha channel and also want only day(night) data.

Yeah that’s what I want to say.

@yukaribbba
Copy link
Contributor Author

Anything else I can do to this PR? @mraspaud @pnuu @gerritholl

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

@mraspaud
Copy link
Member

@yukaribbba thanks for this PR, it looks good to me. However, as @djhoese followed this more closely, I'd like to have his approval before merging this.

@yukaribbba
Copy link
Contributor Author

@mraspaud Okay.😊

Copy link
Member

@djhoese djhoese left a comment

Choose a reason for hiding this comment

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

Looks good. There is one issue with rendering the docs which I think I've fixed in my other comment. I'll commit that in a second.

Otherwise, my only other concern is something I think you've made the right decision on: the coszen is a value between 0 and 1 so for a day-only case you might want different things masked out. There is the night region, the twilight/mixed region, and the day region. If I say include_alpha=False do I want the twilight region removed or should it stay in the image? I think it needs to stay in the image which is what I think you've done here. This way it can be overlayed with a night-only image later on and there is no gap in the middle between the two images.

Right?

satpy/composites/__init__.py Outdated Show resolved Hide resolved
@yukaribbba
Copy link
Contributor Author

If I say include_alpha=False do I want the twilight region removed or should it stay in the image? I think it needs to stay in the image which is what I think you've done here. This way it can be overlayed with a night-only image later on and there is no gap in the middle between the two images.

Sure the twilight region will stay on the image. Here's a result for your case:
image

But, you may have noticed that this area seems to be a little darker since both images are fading there. So if you want a stacked image just simply use the default "day_and_night" and choose two normal composites.

@djhoese
Copy link
Member

djhoese commented Jan 23, 2023

But, you may have noticed that this area seems to be a little darker since both images are fading there. So if you want a stacked image just simply use the default "day_and_night" and choose two normal composites.

Well see in my case (a possible future use, not currently doing this) would be having the individual masked day-only and night-only images and then in tiled image viewer (like https://geosphere.ssec.wisc.edu/) I would allow the user to switch the night image but keep the same day image. Generating all those combinations would not be a realistic option. I suppose what you're saying is that since these images would be "blending to black", they'll always have darker night-time transitions than they would if they were combined from the beginning (in satpy). Ya know, I'm OK with having the alpha channel in my images anyway so this probably isn't a realistic problem for me.

@yukaribbba
Copy link
Contributor Author

Also, talking about this made me think of #2288 . Now we may have a easier solution.

true_color_corr:
lim_low: 85
lim_high: 88
day_night: day_only
include_alpha: false

true_color_uncorr:
lim_low: 85
lim_high: 88
day_night: night_only
include_alpha: false

true_color_mix:
- true_color_corr
- true_color_uncorr
lim_low: 85
lim_high: 88
day_night: day_night

Considering there could be some unexpected noises on night side, this could be cleaner:

true_color_mix_cleaner:
- true_color_mix
lim_low: 85
lim_high: 88
day_night: day_only
include_alpha: false

I have tested this recipe and it’s just perfect for me.

@djhoese
Copy link
Member

djhoese commented Jan 23, 2023

CC @simonrp84 ^

@yukaribbba
Copy link
Contributor Author

I suppose what you're saying is that since these images would be "blending to black", they'll always have darker night-time transitions than they would if they were combined from the beginning (in satpy).

Yep. That’s the point.

@djhoese
Copy link
Member

djhoese commented Jan 23, 2023

Let's merge this and if someone comes up with other use cases that need changes to this implementation we can make additional PRs. Thanks @yukaribbba for putting this together and working with all my requests.

@djhoese djhoese merged commit 955e301 into pytroll:main Jan 23, 2023
@pnuu
Copy link
Member

pnuu commented Jan 23, 2023

Well see in my case (a possible future use, not currently doing this) would be having the individual masked day-only and night-only images and then in tiled image viewer (like https://geosphere.ssec.wisc.edu/) I would allow the user to switch the night image but keep the same day image. Generating all those combinations would not be a realistic option.

Oooh, I need to think about this idea, too!

@yukaribbba
Copy link
Contributor Author

@djhoese @mraspaud Thanks for the help! 😁

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
None yet
Development

Successfully merging this pull request may close these issues.

Alpha band improvement for DayNightCompositor
5 participants