Prevent numerical instability in pt_downscale_radiations #132
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi everyone,
I have experience some numerical instability when downscaling the Shortwave Radiation. I am downscaling ERA5 to a set of representative points in a basin. Some of them have large slope and I think that's why I encountered this problem.
I noticed the problem as in some points I got nonphysical values of SW, caused by a large SW_direct component. Here's an example of a west-facing and 30 degrees steep point:
I get a spike of SW radiation in the evening (sorry for the wrong time axis, times are in UTC but the basin is at UTC-7). That is caused by the fact that the ratio between cos_illumination and mu0 explodes, likely because there is a very small value of mu0 just before the sunset.
I thought of solving it by setting a minimum value of mu0 as 0.25
mu0_stable = mu0.where(mu0 > 0.25, 0.25). That value corresponds to a sun zenith of 75 degrees. I called this mu0_stable and substituted it in the formulas where mu0 appears in the denominator.Moreover, while I was exploring these terms I also discovered that, for some approximation errors I guess, the calculation of surf_interp['SW_direct'] leads in some cases to values slightly larger than the SW radiation on the top of the atmosphere (SWtoa). When that happens, I got that ka was at times negative, as you can see in the next figure (representing another west-facing point but steeper). Negative ka also leads to exploding SW_direct, this time in the formula for down_pt['SW_direct_tmp'].
So I removed the values of surf_interp['SW_direct'] that are larger than SWtoa by substituting them with SWtoa:
surf_interp['SW_direct'][surf_interp['SW_direct'] > SWtoa] = SWtoa[surf_interp['SW_direct'] > SWtoa]I've already discussed about the first issue with @krisaalstad. However, I'm not sure at all that these fixes work well in other cases. I guess there needs to be some testing in other basins before accepting these?
If needed I can provide you with the dataset I'm working with.
Cheers,
Marco.