Skip to content

Commit 64dfe33

Browse files
committed
bc smoothing option added for fwp runtime
1 parent 945ca3a commit 64dfe33

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

sup3r/bias/bias_calc.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class DataRetrievalBase:
3030
"""
3131

3232
def __init__(self, base_fps, bias_fps, base_dset, bias_feature,
33-
target, shape,
34-
base_handler='Resource', bias_handler='DataHandlerNCforCC',
33+
target=None, shape=None, base_handler='Resource',
34+
bias_handler='DataHandlerNCforCC',
3535
bias_handler_kwargs=None, decimals=None):
3636
"""
3737
Parameters
@@ -53,8 +53,10 @@ def __init__(self, base_fps, bias_fps, base_dset, bias_feature,
5353
be a single feature name corresponding to base_dset
5454
target : tuple
5555
(lat, lon) lower left corner of raster to retrieve from bias_fps.
56+
If None then the lower left corner of the full domain will be used.
5657
shape : tuple
57-
(rows, cols) grid size to retrieve from bias_fps.
58+
(rows, cols) grid size to retrieve from bias_fps. If None then the
59+
full domain shape will be used.
5860
base_handler : str
5961
Name of rex resource handler class to be retrieved from the rex
6062
library.
@@ -553,8 +555,8 @@ def _run_single(cls, bias_data, base_fps, bias_feature, base_dset,
553555
bias_feature, base_dset)
554556
return out
555557

556-
def fill_smooth_extend(self, out, fill_extend=True, smooth_extend=0,
557-
smooth_interior=0):
558+
def fill_and_smooth(self, out, fill_extend=True, smooth_extend=0,
559+
smooth_interior=0):
558560
"""Fill data extending beyond the base meta data extent by doing a
559561
nearest neighbor gap fill. Smooth interior and extended region with
560562
given smoothing values.
@@ -580,7 +582,7 @@ def fill_smooth_extend(self, out, fill_extend=True, smooth_extend=0,
580582
deviation for the gaussian_filter kernel
581583
smooth_interior : float
582584
Value to use to smooth the scalar/adder data inside of the spatial
583-
domain set by the threshold input. This can reduce the affect of
585+
domain set by the threshold input. This can reduce the effect of
584586
extreme values within aggregations over large number of pixels.
585587
This value is the standard deviation for the gaussian_filter
586588
kernel.
@@ -599,7 +601,7 @@ def fill_smooth_extend(self, out, fill_extend=True, smooth_extend=0,
599601

600602
arr_smooth = arr[..., idt]
601603

602-
needs_fill = (fill_extend or smooth_extend > 0
604+
needs_fill = (np.isnan(arr_smooth).any() and fill_extend
603605
or smooth_interior > 0)
604606

605607
if needs_fill:
@@ -770,8 +772,8 @@ def run(self, knn, threshold=0.6, fp_out=None, max_workers=None,
770772

771773
logger.info('Finished calculating bias correction factors.')
772774

773-
out = self.fill_smooth_extend(out, fill_extend, smooth_extend,
774-
smooth_interior)
775+
out = self.fill_and_smooth(out, fill_extend, smooth_extend,
776+
smooth_interior)
775777

776778
self.write_outputs(fp_out, out)
777779

sup3r/bias/bias_transforms.py

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
22
"""Bias correction transformation functions."""
3-
import numpy as np
43
import logging
54
from warnings import warn
6-
from rex import Resource
75

6+
import numpy as np
7+
from rex import Resource
8+
from scipy.ndimage.filters import gaussian_filter
89

910
logger = logging.getLogger(__name__)
1011

@@ -37,7 +38,7 @@ def global_linear_bc(input, scalar, adder, out_range=None):
3738

3839

3940
def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
40-
out_range=None):
41+
out_range=None, smoothing=0):
4142
"""Bias correct data using a simple annual (or multi-year) *scalar +adder
4243
method on a site-by-site basis.
4344
@@ -64,6 +65,11 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
6465
source shape will be used.
6566
out_range : None | tuple
6667
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.
6773
6874
Returns
6975
-------
@@ -99,6 +105,15 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
99105
scalar = np.repeat(scalar, input.shape[-1], axis=-1)
100106
adder = np.repeat(adder, input.shape[-1], axis=-1)
101107

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+
102117
out = input * scalar + adder
103118
if out_range is not None:
104119
out = np.maximum(out, np.min(out_range))
@@ -108,7 +123,8 @@ def local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
108123

109124

110125
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):
112128
"""Bias correct data using a simple monthly *scalar +adder method on a
113129
site-by-site basis.
114130
@@ -145,6 +161,11 @@ def monthly_local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
145161
this to False.
146162
out_range : None | tuple
147163
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.
148169
149170
Returns
150171
-------
@@ -189,6 +210,15 @@ def monthly_local_linear_bc(input, feature_name, bias_fp, lr_padded_slice,
189210
logger.warning(msg)
190211
warn(msg)
191212

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+
192222
out = input * scalar + adder
193223
if out_range is not None:
194224
out = np.maximum(out, np.min(out_range))

sup3r/solar/solar.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
"""
88
import glob
99
import json
10+
import logging
1011
import os
12+
1113
import numpy as np
12-
import logging
1314
import pandas as pd
14-
from scipy.spatial import KDTree
1515
from farms.disc import disc
1616
from farms.utilities import calc_dhi, dark_night
17-
from rex import Resource, MultiTimeResource
17+
from rex import MultiTimeResource, Resource
1818
from rex.utilities.fun_utils import get_fun_call_str
19+
from scipy.spatial import KDTree
1920

21+
from sup3r.postprocessing.file_handling import H5_ATTRS, RexOutputs
2022
from sup3r.utilities import ModuleName
21-
from sup3r.postprocessing.file_handling import RexOutputs, H5_ATTRS
22-
2323

2424
logger = logging.getLogger(__name__)
2525

@@ -545,7 +545,6 @@ def write(self, fp_out, features=('ghi', 'dni', 'dhi')):
545545
attrs=attrs,
546546
chunks=attrs['chunks'])
547547
logger.info(f'Added "{feature}" to output file.')
548-
549548
run_attrs = self.gan_data.h5[self._sup3r_fps[0]].global_attrs
550549
run_attrs['nsrdb_source'] = self._nsrdb_fp
551550
fh.run_attrs = run_attrs

sup3r/utilities/utilities.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ def get_input_handler_class(file_paths, input_handler_name):
13051305
A list of files to extract raster data from. Each file must have
13061306
the same number of timesteps. Can also pass a string with a
13071307
unix-style file path which will be passed through glob.glob
1308-
input_handler : str
1308+
input_handler_name : str
13091309
data handler class to use for input data. Provide a string name to
13101310
match a class in data_handling.py. If None the correct handler will
13111311
be guessed based on file type and time series properties.

0 commit comments

Comments
 (0)