1
1
# -*- coding: utf-8 -*-
2
2
"""Bias correction transformation functions."""
3
- import numpy as np
4
3
import logging
5
4
from warnings import warn
6
- from rex import Resource
7
5
6
+ import numpy as np
7
+ from rex import Resource
8
+ from scipy .ndimage .filters import gaussian_filter
8
9
9
10
logger = logging .getLogger (__name__ )
10
11
@@ -37,7 +38,7 @@ def global_linear_bc(input, scalar, adder, out_range=None):
37
38
38
39
39
40
def local_linear_bc (input , feature_name , bias_fp , lr_padded_slice ,
40
- out_range = None ):
41
+ out_range = None , smoothing = 0 ):
41
42
"""Bias correct data using a simple annual (or multi-year) *scalar +adder
42
43
method on a site-by-site basis.
43
44
@@ -64,6 +65,11 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
64
65
source shape will be used.
65
66
out_range : None | tuple
66
67
Option to set floor/ceiling values on the output data.
68
+ smoothing : float
69
+ Value to use to smooth the scalar/adder data. This can reduce the
70
+ effect of extreme values within aggregations over large number of
71
+ pixels. This value is the standard deviation for the gaussian_filter
72
+ kernel.
67
73
68
74
Returns
69
75
-------
@@ -99,6 +105,15 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
99
105
scalar = np .repeat (scalar , input .shape [- 1 ], axis = - 1 )
100
106
adder = np .repeat (adder , input .shape [- 1 ], axis = - 1 )
101
107
108
+ if smoothing > 0 :
109
+ for idt in range (scalar .shape [- 1 ]):
110
+ scalar [..., idt ] = gaussian_filter (scalar [..., idt ],
111
+ smoothing ,
112
+ mode = 'nearest' )
113
+ adder [..., idt ] = gaussian_filter (adder [..., idt ],
114
+ smoothing ,
115
+ mode = 'nearest' )
116
+
102
117
out = input * scalar + adder
103
118
if out_range is not None :
104
119
out = np .maximum (out , np .min (out_range ))
@@ -108,7 +123,8 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
108
123
109
124
110
125
def monthly_local_linear_bc (input , feature_name , bias_fp , lr_padded_slice ,
111
- time_index , temporal_avg = True , out_range = None ):
126
+ time_index , temporal_avg = True , out_range = None ,
127
+ smoothing = 0 ):
112
128
"""Bias correct data using a simple monthly *scalar +adder method on a
113
129
site-by-site basis.
114
130
@@ -145,6 +161,11 @@ def monthly_local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
145
161
this to False.
146
162
out_range : None | tuple
147
163
Option to set floor/ceiling values on the output data.
164
+ smoothing : float
165
+ Value to use to smooth the scalar/adder data. This can reduce the
166
+ effect of extreme values within aggregations over large number of
167
+ pixels. This value is the standard deviation for the gaussian_filter
168
+ kernel.
148
169
149
170
Returns
150
171
-------
@@ -189,6 +210,15 @@ def monthly_local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
189
210
logger .warning (msg )
190
211
warn (msg )
191
212
213
+ if smoothing > 0 :
214
+ for idt in range (scalar .shape [- 1 ]):
215
+ scalar [..., idt ] = gaussian_filter (scalar [..., idt ],
216
+ smoothing ,
217
+ mode = 'nearest' )
218
+ adder [..., idt ] = gaussian_filter (adder [..., idt ],
219
+ smoothing ,
220
+ mode = 'nearest' )
221
+
192
222
out = input * scalar + adder
193
223
if out_range is not None :
194
224
out = np .maximum (out , np .min (out_range ))
0 commit comments