Reproject INCA data¶
In this jupyter notebook, we will show how to import and reproject a precipitation forecast from the INCA nowcasting system.
Install dependencies¶
First, let’s set up our working environment. It is recommended to use a virtual environment.
Important: In Google Colab, this notebook needs to be executed one cell at a time. Trying to excecute all the cells at once may results in cells being skipped and some dependencies not being installed.
Now, let’s install the dependendies that will allow us to read and visualize the example data. We will need xarray to work with labelled multi-dimensional arrays, pyproj for coordinate transformations, and rioxarray to extend xarray with methods from rasterio for geospatial datasets.
[1]:
!pip install xarray pyproj rioxarray
Requirement already satisfied: xarray in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (0.20.2)
Requirement already satisfied: pyproj in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (3.3.0)
Collecting rioxarray
Downloading rioxarray-0.9.0.tar.gz (46 kB)
|████████████████████████████████| 46 kB 2.5 MB/s
Installing build dependencies ... - \ | / done
Getting requirements to build wheel ... - done
Preparing metadata (pyproject.toml) ... - done
Requirement already satisfied: numpy>=1.18 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from xarray) (1.20.3)
Requirement already satisfied: pandas>=1.1 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from xarray) (1.3.5)
Requirement already satisfied: certifi in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from pyproj) (2021.10.8)
Requirement already satisfied: packaging in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from rioxarray) (21.3)
Collecting rasterio
Downloading rasterio-1.2.10-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (19.2 MB)
|████████████████████████████████| 19.2 MB 7.9 MB/s
Requirement already satisfied: pytz>=2017.3 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from pandas>=1.1->xarray) (2021.3)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from pandas>=1.1->xarray) (2.8.2)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from packaging->rioxarray) (3.0.6)
Collecting snuggs>=1.4.1
Downloading snuggs-1.4.7-py3-none-any.whl (5.4 kB)
Requirement already satisfied: setuptools in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from rasterio->rioxarray) (59.6.0)
Collecting cligj>=0.5
Downloading cligj-0.7.2-py3-none-any.whl (7.1 kB)
Collecting click-plugins
Downloading click_plugins-1.1.1-py2.py3-none-any.whl (7.5 kB)
Requirement already satisfied: attrs in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from rasterio->rioxarray) (21.2.0)
Collecting affine
Downloading affine-2.3.0-py2.py3-none-any.whl (15 kB)
Collecting click>=4.0
Downloading click-8.0.3-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 10.9 MB/s
Requirement already satisfied: six>=1.5 in /home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas>=1.1->xarray) (1.16.0)
Building wheels for collected packages: rioxarray
Building wheel for rioxarray (pyproject.toml) ... - done
Created wheel for rioxarray: filename=rioxarray-0.9.0-py3-none-any.whl size=54395 sha256=40110b1c3ca1d00f378137174f30b4d3d00d39f81049031bb493572cc1f9d240
Stored in directory: /home/docs/.cache/pip/wheels/c8/50/a8/e32a8a696766ede6d22e68f1f8dede396b7999ca40576a5d2e
Successfully built rioxarray
Installing collected packages: click, snuggs, cligj, click-plugins, affine, rasterio, rioxarray
Successfully installed affine-2.3.0 click-8.0.3 click-plugins-1.1.1 cligj-0.7.2 rasterio-1.2.10 rioxarray-0.9.0 snuggs-1.4.7
Download the data¶
Now that we have the environment ready, let’s download an example dataset.
On Zenodo, we can download the ouput from a single run of INCA for precipitation (mm/h).
[2]:
!wget https://zenodo.org/record/5575197/files/RR_INCA.nc
--2021-12-15 18:01:58-- https://zenodo.org/record/5575197/files/RR_INCA.nc
Resolving zenodo.org (zenodo.org)... 137.138.76.77
Connecting to zenodo.org (zenodo.org)|137.138.76.77|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5887704 (5.6M) [application/octet-stream]
Saving to: ‘RR_INCA.nc.1’
RR_INCA.nc.1 100%[===================>] 5.61M 5.58MB/s in 1.0s
2021-12-15 18:02:01 (5.58 MB/s) - ‘RR_INCA.nc.1’ saved [5887704/5887704]
Read the data¶
Now that we have downloaded the example data, let’s import the example netCDF with xarray.open_dataset()
method.
The file is called RR_INCA.nc
. “RR” is the product name used to identify the quantitative precipitation nowcasting product based on the merging between radar and station observations (named CombiPrecip). This product is in units of mm/h, it is updated every 10 minutes, and it has a 6 hour forecast range.
We import this 6-hour precipitation forecast, which corresponds to a sequence of 37 frames of 10-min precipitation fields (1 analysis and 36 forecasts).
[3]:
import xarray as xr
ds = xr.open_dataset("RR_INCA.nc")
ds
[3]:
<xarray.Dataset> Dimensions: (chx: 710, chy: 640, time: 37) Coordinates: * chx (chx) float64 2.555e+05 2.565e+05 ... 9.635e+05 9.645e+05 * chy (chy) float64 -1.595e+05 -1.585e+05 ... 4.785e+05 4.795e+05 * time (time) datetime64[ns] 2021-07-08T08:20:00 ... 2021-07-08T14... Data variables: RR (time, chy, chx) float32 ... grid_mapping float32 9.969e+36 Attributes: conventions: CF-1.6 conventionsURL: http://cfconventions.org/cf-conventions/v1.6.0/cf-conven... title: simple NetCDF output institution: MeteoSwiss (NMC Switzerland) source: model: inca-1,product_category: determinist, version: 3.2 reference: MeteoSwiss (2021) history: Produced by inca2netcdf 1.0 on 2021-07-08 08:30 UTC comment: Illustrate simplified NetCDF structure when all fields d...
Reproject with pyproj¶
The Swiss CH1903 / LV03 coordinates (EPSG 21781) can be reprojected to any other reference system using pyproj. In the example below, we’ll use WGS84 as the target projection (EPSG 4326).
Note that as a result, the dataset won’t be anymore on a regular grid.
[4]:
import numpy as np
from pyproj import Transformer
chxv, chyv = np.meshgrid(ds.chx, ds.chy)
transformer = Transformer.from_crs(
"epsg:21781",
"epsg:4326",
always_xy=True,
)
lon, lat = transformer.transform(chxv, chyv)
ds = ds.assign_coords({"lon": (("chy", "chx"), lon) , "lat": (("chy", "chx"), lat)})
ds.RR.isel(time=0).plot.pcolormesh("lon", "lat") # not a regular grid anymore
[4]:
<matplotlib.collections.QuadMesh at 0x7feb84be70a0>
Reproject and resample with rioxarray¶
The rioxarray library extends xarray with methods based on rasterio to handle georefenced dataset. This makes it very easy to reproject and resample datasets between different CRS.
[5]:
import rioxarray # for the extension to load
ds = xr.open_dataset("RR_INCA.nc")
ds = ds.rio.write_crs("epsg:21781")
ds_latlon = ds.rio.reproject("epsg:4326", resolution=0.01)
ds_latlon.RR.isel(time=0).plot() # still a regular grid
ds_latlon
/home/docs/checkouts/readthedocs.org/user_builds/inca-examples/conda/latest/lib/python3.9/site-packages/pyproj/crs/_cf1x8.py:511: UserWarning: angle from rectified to skew grid parameter lost in conversion to CF
warnings.warn(
[5]:
<xarray.Dataset> Dimensions: (x: 977, y: 585, time: 37) Coordinates: * x (x) float64 2.694 2.704 2.714 2.724 ... 12.43 12.44 12.45 * y (y) float64 49.46 49.45 49.44 49.43 ... 43.64 43.63 43.62 * time (time) datetime64[ns] 2021-07-08T08:20:00 ... 2021-07-08T14... grid_mapping int64 0 Data variables: RR (time, y, x) float32 nan nan nan nan nan ... nan nan nan nan Attributes: conventions: CF-1.6 conventionsURL: http://cfconventions.org/cf-conventions/v1.6.0/cf-conven... title: simple NetCDF output institution: MeteoSwiss (NMC Switzerland) source: model: inca-1,product_category: determinist, version: 3.2 reference: MeteoSwiss (2021) history: Produced by inca2netcdf 1.0 on 2021-07-08 08:30 UTC comment: Illustrate simplified NetCDF structure when all fields d...