<jupyter_start><jupyter_text>[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/MeteoSwiss/inca-examples/blob/main/notebooks/reprojecting_inca.ipynb) Reproject INCA dataIn this jupyter notebook, we will show how to import and reproject a precipitationforecast from the INCA [nowcasting system](https://www.meteoswiss.admin.ch/home/services-and-publications/produkte.subpage.html/en/data/products/2021/nowcasting-inca-ch.html). Install dependenciesFirst, let's set up our working environment. It is recommended to use a virtualenvironment.**Important**: In Google Colab, this notebook needs to be executed one cellat 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](http://xarray.pydata.org) to work withlabelled multi-dimensional arrays, [pyproj](https://github.com/pyproj4/pyproj)for coordinate transformations, and [rioxarray](https://github.com/corteva/rioxarray)to extend xarray with methods from [rasterio](https://rasterio.readthedocs.io) for geospatial datasets.<jupyter_code>!pip install xarray pyproj rioxarray<jupyter_output><empty_output><jupyter_text>Download the dataNow that we have the environment ready, let's download an example dataset.On [Zenodo](https://zenodo.org/record/5575197), we can download the ouput from asingle run of INCA for precipitation (mm/h).<jupyter_code>!wget https://zenodo.org/record/5575197/files/RR_INCA.nc<jupyter_output><empty_output><jupyter_text>Read the dataNow 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 identifythe quantitative precipitation nowcasting product based on the merging betweenradar and station observations (named CombiPrecip). This product is in units ofmm/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).<jupyter_code>import xarray as xr

ds = xr.open_dataset("RR_INCA.nc")
ds<jupyter_output><empty_output><jupyter_text>Reproject with pyprojThe Swiss CH1903 / LV03 coordinates ([EPSG 21781](https://epsg.io/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](https://epsg.io/4326)).Note that as a result, the dataset won't be anymore on a regular grid.<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Reproject and resample with rioxarrayThe 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.<jupyter_code>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<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10090734121528733, 'fraction_numerical': 0.021721198790211713, 'mean_word_length': 5.890151515151516, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 10, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '285122', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1134, 'n_words': 362, 'path': 'notebooks/reprojecting_inca.ipynb', 'repo_name': 'MeteoSwiss/inca-examples', 'size': '3637'}