-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add spatial disaggregation recipe #57
Open
dgergel
wants to merge
12
commits into
pangeo-data:main
Choose a base branch
from
dgergel:feature/add_spatial_disaggregation_recipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
104f1ac
add first part of recipe
dgergel e599e75
add remainder of recipe and code for spatialdisaggregator class
dgergel aa690d0
fix formatting
dgergel 098d8ea
Merge branch 'master' of https://github.com/jhamman/scikit-downscale …
dgergel 0b26592
formatting cleanup
dgergel 78a9d17
more formatting updates
dgergel 16a83c1
flake8 change
dgergel 97b1594
test pre-commit fix
dgergel 4edee63
fix precommit problems
dgergel de0bf29
Merge branch 'main' of github.com:jhamman/scikit-downscale into featu…
dgergel b4e0262
add unit tests for SpatialDisaggregator
dgergel 458f292
updates per review suggestions
dgergel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .regridding import apply_weights | ||
from .sd import SpatialDisaggregator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import xesmf as xe | ||
|
||
|
||
def apply_weights(regridder, input_data): | ||
regridder._grid_in = None | ||
regridder._grid_out = None | ||
result = regridder(input_data) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import xarray as xr | ||
|
||
|
||
class SpatialDisaggregator: | ||
""" | ||
Spatial disaggregation model class | ||
Apply spatial disaggregation algorithm to an xarray Dataset with fit | ||
and predict methods using NASA-NEX method for spatial disaggregation | ||
(see Thrasher et al, 2012). | ||
|
||
Parameters | ||
---------- | ||
var : str | ||
specifies the variable being downscaled. Default is | ||
temperature and other option is precipitation. | ||
""" | ||
|
||
def __init__(self, var="temperature"): | ||
self._var = var | ||
|
||
if var not in ['temperature', 'precipitation']: | ||
raise NotImplementedError( | ||
"functionality for spatial disaggregation" | ||
" of %s has not yet been added" % var | ||
) | ||
|
||
def fit(self, ds_bc, climo_coarse, var_name, lat_name="lat", lon_name="lon"): | ||
""" | ||
Fit the scaling factor used in spatial disaggregation | ||
|
||
Parameters | ||
----------- | ||
ds_bc : xarray.Dataset | ||
Daily bias corrected data at the model resolution | ||
climo_coarse : xarray.DataArray or xarray.Dataset | ||
Observed climatology that has been regridded (coarsened) | ||
to the model resolution | ||
var_name : str | ||
Specifies the data variable name within ds_bc of the | ||
daily bias corrected data | ||
lat_name : str | ||
Name of the latitude dimension of ds_bc and climo_coarse, | ||
default is 'lat'. | ||
lon_name : str | ||
Name of the longitude dimension of ds_bc and climo_coarse, | ||
default is 'lon'. | ||
""" | ||
|
||
# check that climo has been regridded to model res | ||
if not ds_bc[lat_name].equals(climo_coarse[lat_name]): | ||
raise ValueError("climo latitude dimension does not match model res") | ||
if not ds_bc[lon_name].equals(climo_coarse[lon_name]): | ||
raise ValueError("climo longitude dimension does not match model res") | ||
|
||
scf = self._calculate_scaling_factor(ds_bc, climo_coarse, var_name, self._var) | ||
|
||
return scf | ||
|
||
def predict(self, scf, climo_fine, var_name, lat_name="lat", lon_name="lon"): | ||
""" | ||
Predict (apply) the scaling factor to the observed climatology. | ||
|
||
Parameters | ||
----------- | ||
scf : xarray.Dataset | ||
Scale factor that has been regridded to the resolution of | ||
the observed data. | ||
climo_fine : xarray.DataArray or xarray.Dataset | ||
Observed climatology at its native resolution | ||
var_name : str | ||
Specifies the data variable name within ds_bc of the | ||
daily bias corrected data | ||
lat_name : str | ||
Name of the latitude dimension of ds_bc and climo_coarse, | ||
default is 'lat'. | ||
lon_name : str | ||
Name of the longitude dimension of ds_bc and climo_coarse, | ||
default is 'lon' | ||
""" | ||
|
||
# check that scale factor has been regridded to obs res | ||
if not np.array_equal(scf[lat_name], climo_fine[lat_name]): | ||
raise ValueError("scale factor latitude dimension does not match obs res") | ||
if not np.array_equal(scf[lon_name], climo_fine[lon_name]): | ||
raise ValueError("scale factor longitude dimension does not match obs res") | ||
|
||
downscaled = self._apply_scaling_factor(scf, climo_fine, var_name, self._var) | ||
|
||
return downscaled | ||
|
||
def _calculate_scaling_factor(self, ds_bc, climo, var_name, var): | ||
""" | ||
compute scaling factor | ||
""" | ||
# Necessary workaround to xarray's check with zero dimensions | ||
# https://github.com/pydata/xarray/issues/3575 | ||
da = ds_bc[var_name] | ||
if sum(da.shape) == 0: | ||
return da | ||
groupby_type = ds_bc.time.dt.dayofyear | ||
gb = da.groupby(groupby_type) | ||
|
||
if var == "temperature": | ||
return gb - climo | ||
elif var == "precipitation": | ||
return gb / climo | ||
|
||
def _apply_scaling_factor(self, scf, climo, var_name, var): | ||
""" | ||
apply scaling factor | ||
""" | ||
groupby_type = scf.time.dt.dayofyear | ||
da = scf[var_name] | ||
sff_daily = da.groupby(groupby_type) | ||
|
||
if var == "temperature": | ||
return sff_daily + climo | ||
elif var == "precipitation": | ||
return sff_daily * climo |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit but these are over-indented (same for all docstrings here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fixed now.