When you want to process raster data (arrays with geo-coordinates) with Python, there are several options. It was the most straightforward for me to use Python, read all the data in xarray and process it with rasterio using rioxarray (tutorial by University of Colorado Boulder here).
Rioxarray requires data arrays, x-y coordinate array, and coordinate reference system information (CRS) to function. However, it is not always easy to find out and set the CRS, as different files stores CRS in different places (I wish for more advancement of data-standardization!).
In this article, I summarize 4 ways to find out and set the CRS in a xarray dataset.
Disclaimer: I am not the super expert on the projection system, so please use the code at your own risk and let me know when you find any mistakes!
To start off, I import libraries as follows.
Method 1. Get CRS info with gdal (the easiest option)
If the data is well-prepared for geo-processing, you will not need to dig into the data file. Gdal will do the job.
Method 2. Read a wkt or proj4 string from a file
If the Method 1 does not work, you have to dig into the data file and find out where the CRS information is located.
Method 3. Read CF from a file, and convert it to CRS
Similar to the second method. However, sometimes in the NetCDF file, the coordinate information is stored in the CF convention, which need conversion to CRS (details in pyproj website).
Method 4. Write a wkt or proj4 string
Last, if the above all did not work, you can create a CRS string by yourself. Find out where the projection, the parameters for the projection, the datum, and the spheroid are located.
Here are some useful links for this method.
To find out the compatibility of different parameter names for common projection: https://cfconventions.org/wkt-proj-4.html
To find out the formatting template for wkt or proj4 strings (you will only need to tweak the parameters): http://epsg.io/4326
By the way, specifically for the WRF-Hydro grid, I wrote as follows (referring to this blogpost). WRF-Hydro is national hydrologic model in the U.S., and it uses their own grid definitions.
Hope it helps!!