Skip to content
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

Bias correction #292

Merged
merged 19 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
707b263
Started updating xclim package dependencies
richardarsenault Jun 15, 2020
e817f7b
add test file for bias correction
richardarsenault Jun 15, 2020
0c57bb4
Update a few calls and adjust dependencies to new xclim calls
Zeitsperre Jun 15, 2020
b5a3566
try and trigger bias_correction branch build
Zeitsperre Jun 15, 2020
a125ff4
temporary fix top satisfy xclim.sdba requirements of xarray@master
Zeitsperre Jun 15, 2020
26d62d1
Merge pull request #281 from Ouranosinc/bc_dependency_fix
Zeitsperre Jun 16, 2020
a17dc6d
fixed bias correction function call
richardarsenault Jun 27, 2020
d0849e7
WIP problem with Nans from xarray mean
richardarsenault Jul 11, 2020
3f09f0a
Bias correction code works but has limitations
richardarsenault Jul 11, 2020
4fbb8df
Modifiying start date that causes NetCDF DAP failure
richardarsenault Jul 17, 2020
687e203
environment.yml: sync gdal version with requirements.txt
tlvu Jul 20, 2020
f187dde
Merge pull request #290 from Ouranosinc/fix-broken-make-develop
tlvu Jul 20, 2020
e129a39
tests + notebooks: use new official NCML link
tlvu Jul 21, 2020
66b67e3
Merge pull request #291 from Ouranosinc/use-official-ncml-link
tlvu Jul 22, 2020
dc53a33
Merge remote-tracking branch 'origin/master' into bias_correction
tlvu Jul 22, 2020
73b2949
blacken test_bias_correction.py for pep8
tlvu Jul 22, 2020
32cd217
test_bias_correction.py: pep8
tlvu Jul 22, 2020
a08cc05
.travis.yml: remove branch bias_correction from build branch
tlvu Jul 24, 2020
11b7249
.travis.yml: remove xarray install from source
tlvu Jul 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: generic

os: linux
dist: xenial

jobs:
Expand All @@ -15,8 +15,7 @@ jobs:
update: true
env:
- CONDA_FN="Miniconda3-latest-MacOSX-x86_64.sh"
- os: linux
name: "Linux (Python3.6 + pep8)"
- name: "Linux (Python3.6 + pep8)"
dist: xenial
python: "3.6"
addons:
Expand All @@ -29,18 +28,15 @@ jobs:
- os: linux
name: "Linux (Python3.7 + docs)"
python: "3.7"
dist: xenial
addons:
apt:
packages:
- libnetcdf-dev
env:
- CONDA_FN="Miniconda3-latest-Linux-x86_64.sh"
- DOCS=true
- os: linux
name: "Linux (Python3.8 + notebooks)"
- name: "Linux (Python3.8 + notebooks)"
python: "3.8"
dist: xenial
addons:
apt:
packages:
Expand All @@ -56,6 +52,7 @@ jobs:
branches:
only:
- master
- bias_correction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to limit Travis-CI to only master branch, then we have to add each dev branch manually, like this bias_correction branch? @Zeitsperre (b5a3566)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed. It was placed in order to trigger the bias_correction branch that PRs were merging to in a few instances. The reason why I set only to master is that otherwise we tend to get two duplicate sets of builds; one that is the pushes set against master and another that is the pushes set against the last version of master it stemmed from.

Since we only care about the current state of master, as all pushes must integrate these changes, there's nothing to gain except more redundant build checks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the redundant build checks on master branch will happen only once, when the PR is merged to master.

Whereas instructions to build only the master branch will not test any commits on any devel branches and force all devel branches to hardcode the devel branch name like we just saw.

I think having the redundant build on master is a less bad of the 2 problems. Wonder what's your opinion about removing this "build only master branch" instruction for all birds?


install:
- make bootstrap
Expand All @@ -75,8 +72,9 @@ install:

before_script:
# # Start WPS service on port 5000 on 0.0.0.0
# - raven start --daemon --bind-host 0.0.0.0 --port 5000
- bash -c "source $HOME/miniconda3/bin/activate raven && make start"
# - raven start --daemon --bind-host 0.0.0.0 --port 5000
- bash -c "source $HOME/miniconda3/bin/activate raven && pip install git+https://github.com/pydata/xarray.git@master"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this install from xarray.git@master still necessary with xarray 0.16 from master branch? @Zeitsperre (a125ff4)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With xarray at 0.16, the sdba functions that Pascal was working on are integrated, so no. This workaround can blissfully be thrown in the trash.

- bash -c "source $HOME/miniconda3/bin/activate raven && make start"

script:
- bash -c "source $HOME/miniconda3/bin/activate raven && make test"
Expand Down
4 changes: 2 additions & 2 deletions docs/source/notebooks/gridded_data_subset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
" \n",
" if not tsfile.exists():\n",
" # Path to unified NetCDF ML dataset file on the THREDDS server (OPeNDAP link)\n",
" NRCAN_url='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/birdhouse/1-Datasets/gridded_obs/nrcan_v2.ncml'\n",
" NRCAN_url='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/gridded_obs/nrcan_v2.ncml'\n",
"\n",
" #Open the dataset file and slice the desired lat/lon (+1°Buffer) and limit to the time simulation duration\n",
" ds=xr.open_dataset(NRCAN_url).sel(lat=slice(lat_max+1,lat_min-1), lon=slice(lon_min-1,lon_max+1), time=slice(start, stop))\n",
Expand Down Expand Up @@ -160,7 +160,7 @@
" tsfile=tmp / 'ERA5_ts.nc'\n",
" day = dt.timedelta(days=1)\n",
" if not tsfile.exists(): \n",
" ERA5_url='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/birdhouse/1-Datasets/reanalyses/era5.ncml'\n",
" ERA5_url='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/reanalyses/era5.ncml'\n",
" ds=xr.open_dataset(ERA5_url).sel(latitude=slice(lat_max+1,lat_min-1), longitude=slice(lon_min+360-1,lon_max+360+1),time=slice(start - day, stop + day))\n",
"\n",
" # Special treatment for ERA5 in North America: ECMWF stores ERA5 longitude in 0:360 format rather than -180:180. We need to reassign the longitudes here\n",
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
- geojson
- netcdf4
- libnetcdf #==4.6.2 # Avoid core dump with 4.7.1. Can be unpinned when 4.7.1 is not the latest anymore.
- gdal~=2.4
- gdal==3.0.4
- fiona
- pyproj
- descartes
Expand Down
10 changes: 7 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spotpy
statsmodels
# GIS LIBRARIES
# pycrs --- Depends on online database requests --> SLOW
gdal~=2.4
# gdal~=2.4
pysheds
affine
rtree
Expand All @@ -33,10 +33,14 @@ shapely
unidecode
geojson
pandoc
ipython
ipyleaflet
ipython # needed for the parsing of Jupyter Notebooks
salem # needed for figures in some notebooks
# salem # needed for figures in some notebooks
psycopg2 # to use postgres to log pywps requests like in Prod
# xclim It seems to be installed by pip in src/ and confuse pytest.
xskillscore
bokeh
distributed
partd
fsspec
gdal==3.0.4
2 changes: 1 addition & 1 deletion tests/test_NRCAN_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from raven.models import HMETS


NRCAN_path = "https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/birdhouse/1-Datasets/gridded_obs/nrcan_v2.ncml"
NRCAN_path = "https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/gridded_obs/nrcan_v2.ncml"

# Temporary path
filepath = tempfile.mkdtemp() + "/NRCAN_ts.nc"
Expand Down
34 changes: 34 additions & 0 deletions tests/test_bias_correction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import datetime as dt
import xarray as xr
import xclim.sdba as sdba


class TestBiasCorrect:
def test_bias_correction(self):

ref_data='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/simulations/cmip5/atmos/day_MPI-ESM-LR_historical.ncml'
fut_data='https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/simulations/cmip5/atmos/day_MPI-ESM-LR_historical+rcp85.ncml'
hist_data="https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/datasets/gridded_obs/nrcan_v2.ncml"

lat=54.484
lon=-123.36

# CAREFUL! ERA5 IS NOT ThE SAME LONGITUDE
# Also, latitude goes from high to low, so I need to have it go from lat+1 to lat-1 in the slice.
# For the NRCan dataset, I cannot have more than about 10 years as I get a "NetCDF: DAP failure" which I think is related to a timeout.
ds = (xr.open_dataset(hist_data).sel(lat=slice(lat + 1, lat - 1),lon=slice(lon - 1, lon + 1), time=slice(dt.datetime(1991,1,1), dt.datetime(2010,12,31))).mean(dim={"lat", "lon"}, keep_attrs=True))

# For lon in 0-360 format, need to add an auto-checker.
lon = 260
ds2 = (xr.open_dataset(ref_data).sel(lat=slice(lat - 1, lat + 1),lon=slice(lon - 1, lon + 1), time=slice(dt.datetime(1981,1,1), dt.datetime(2010,12,31))).mean(dim={"lat", "lon"}, keep_attrs=True))
ds3 = (xr.open_dataset(fut_data).sel(lat=slice(lat - 1, lat + 1),lon=slice(lon - 1, lon + 1), time=slice(dt.datetime(2041,1,1), dt.datetime(2070,12,31))).mean(dim={"lat", "lon"}, keep_attrs=True))

# Here data in ds, ds2 and ds3 are NaN!

group_month_nowindow = sdba.utils.Grouper('time.month')
Adj = sdba.DetrendedQuantileMapping(nquantiles=50, kind='+', group=group_month_nowindow)
Adj.train(ds['pr'],ds2['pr'])
Scen = Adj.adjust(ds3['pr'], interp="linear")
Adj.ds.af # adjustment factors.

print(Adj.ds.af)