@@ -553,9 +553,15 @@ def _run_single(cls, bias_data, base_fps, bias_feature, base_dset,
553
553
bias_feature , base_dset )
554
554
return out
555
555
556
- def fill_extend (self , out , smooth_extend ):
556
+ def fill_smooth_extend (self , out , fill_extend = True , smooth_extend = 0 ,
557
+ smooth_interior = 0 ):
557
558
"""Fill data extending beyond the base meta data extent by doing a
558
- nearest neighbor gap fill.
559
+ nearest neighbor gap fill. Smooth interior and extended region with
560
+ given smoothing values.
561
+ Interior smoothing can reduce the affect of extreme values
562
+ within aggregations over large number of pixels.
563
+ The interior is assumed to be defined by the region without nan values.
564
+ The extended region is assumed to be the region with nan values.
559
565
560
566
Parameters
561
567
----------
@@ -564,11 +570,20 @@ def fill_extend(self, out, smooth_extend):
564
570
data and the scalar + adder factors to correct the biased data
565
571
like: bias_data * scalar + adder. Each value is of shape
566
572
(lat, lon, time).
573
+ fill_extend : bool
574
+ Whether to fill data extending beyond the base meta data with
575
+ nearest neighbor values.
567
576
smooth_extend : float
568
577
Option to smooth the scalar/adder data outside of the spatial
569
578
domain set by the threshold input. This alleviates the weird seams
570
579
far from the domain of interest. This value is the standard
571
580
deviation for the gaussian_filter kernel
581
+ smooth_interior : float
582
+ 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
584
+ extreme values within aggregations over large number of pixels.
585
+ This value is the standard deviation for the gaussian_filter
586
+ kernel.
572
587
573
588
Returns
574
589
-------
@@ -581,12 +596,30 @@ def fill_extend(self, out, smooth_extend):
581
596
for key , arr in out .items ():
582
597
nan_mask = np .isnan (arr [..., 0 ])
583
598
for idt in range (self .NT ):
584
- arr [..., idt ] = nn_fill_array (arr [..., idt ])
599
+
600
+ arr_smooth = arr [..., idt ]
601
+
602
+ needs_fill = (fill_extend or smooth_extend > 0
603
+ or smooth_interior > 0 )
604
+
605
+ if needs_fill :
606
+ arr_smooth = nn_fill_array (arr_smooth )
607
+
608
+ arr_smooth_int = arr_smooth_ext = arr_smooth
609
+
585
610
if smooth_extend > 0 :
586
- arr_smooth = gaussian_filter (arr [..., idt ],
587
- smooth_extend ,
588
- mode = 'nearest' )
589
- out [key ][nan_mask , idt ] = arr_smooth [nan_mask ]
611
+ arr_smooth_ext = gaussian_filter (arr_smooth_ext ,
612
+ smooth_extend ,
613
+ mode = 'nearest' )
614
+
615
+ if smooth_interior > 0 :
616
+ arr_smooth_int = gaussian_filter (arr_smooth_int ,
617
+ smooth_interior ,
618
+ mode = 'nearest' )
619
+
620
+ out [key ][nan_mask , idt ] = arr_smooth_ext [nan_mask ]
621
+ out [key ][~ nan_mask , idt ] = arr_smooth_int [~ nan_mask ]
622
+
590
623
return out
591
624
592
625
def write_outputs (self , fp_out , out ):
@@ -623,7 +656,8 @@ def write_outputs(self, fp_out, out):
623
656
.format (fp_out ))
624
657
625
658
def run (self , knn , threshold = 0.6 , fp_out = None , max_workers = None ,
626
- daily_reduction = 'avg' , fill_extend = True , smooth_extend = 0 ):
659
+ daily_reduction = 'avg' , fill_extend = True , smooth_extend = 0 ,
660
+ smooth_interior = 0 ):
627
661
"""Run linear correction factor calculations for every site in the bias
628
662
dataset
629
663
@@ -654,6 +688,10 @@ def run(self, knn, threshold=0.6, fp_out=None, max_workers=None,
654
688
domain set by the threshold input. This alleviates the weird seams
655
689
far from the domain of interest. This value is the standard
656
690
deviation for the gaussian_filter kernel
691
+ smooth_interior : float
692
+ Option to smooth the scalar/adder data within the valid spatial
693
+ domain. This can reduce the affect of extreme values within
694
+ aggregations over large number of pixels.
657
695
658
696
Returns
659
697
-------
@@ -732,8 +770,8 @@ def run(self, knn, threshold=0.6, fp_out=None, max_workers=None,
732
770
733
771
logger .info ('Finished calculating bias correction factors.' )
734
772
735
- if fill_extend :
736
- out = self . fill_extend ( out , smooth_extend )
773
+ out = self . fill_smooth_extend ( out , fill_extend , smooth_extend ,
774
+ smooth_interior )
737
775
738
776
self .write_outputs (fp_out , out )
739
777
0 commit comments