88import logging
99from skdownscale .spatial_models import SpatialDisaggregator
1010import xarray as xr
11- from xclim import sdba
11+ from xclim import sdba , set_options
1212from xclim .sdba .utils import equally_spaced_nodes
1313from xclim .core .calendar import convert_calendar
1414import xesmf as xe
@@ -53,7 +53,7 @@ def train_quantiledeltamapping(
5353
5454
5555def adjust_quantiledeltamapping_year (
56- simulation , qdm , year , variable , halfyearwindow_n = 10
56+ simulation , qdm , year , variable , halfyearwindow_n = 10 , include_quantiles = False
5757):
5858 """Apply QDM to adjust a year within a simulation.
5959
@@ -74,6 +74,9 @@ def adjust_quantiledeltamapping_year(
7474 halfyearwindow_n : int, optional
7575 Half-length of the annual rolling window to extract along either
7676 side of `year`.
77+ include_quantiles : bool, optional
78+ Whether or not to output quantiles (sim_q) as a coordinate on
79+ the bias corrected data variable in output.
7780
7881 Returns
7982 -------
@@ -96,7 +99,14 @@ def adjust_quantiledeltamapping_year(
9699 simulation = simulation [variable ].sel (
97100 time = timeslice
98101 ) # TODO: Need a check to ensure we have all the data in this slice!
99- out = qdm .adjust (simulation , interp = "nearest" ).sel (time = str (year ))
102+ if include_quantiles :
103+ # include quantile information in output
104+ with set_options (sdba_extra_output = True ):
105+ out = qdm .adjust (simulation , interp = "nearest" ).sel (time = str (year ))
106+ # make quantiles a coordinate of bias corrected output variable
107+ out = out ["scen" ].assign_coords (sim_q = out .sim_q )
108+ else :
109+ out = qdm .adjust (simulation , interp = "nearest" ).sel (time = str (year ))
100110
101111 return out .to_dataset (name = variable )
102112
0 commit comments