{"id":47,"date":"2023-03-08T02:05:05","date_gmt":"2023-03-08T02:05:05","guid":{"rendered":"http:\/\/rarakihydro.com\/?p=47"},"modified":"2025-07-29T23:54:25","modified_gmt":"2025-07-29T23:54:25","slug":"4-ways-to-find-out-coordinate-reference-system-information-crs-and-set-it-in-a-xarray","status":"publish","type":"post","link":"https:\/\/rarakihydro.com\/index.php\/2023\/03\/08\/4-ways-to-find-out-coordinate-reference-system-information-crs-and-set-it-in-a-xarray\/","title":{"rendered":"4 ways to find out Coordinate Reference System information (CRS) and set it in a xarray"},"content":{"rendered":"\n<p>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 <a href=\"http:\/\/xarray.pydata.org\/en\/stable\/\">xarray <\/a>and process it with <a href=\"https:\/\/rasterio.readthedocs.io\/en\/latest\/\">rasterio <\/a>using <a href=\"https:\/\/corteva.github.io\/rioxarray\/stable\/\">rioxarray<\/a> (<a href=\"https:\/\/www.earthdatascience.org\/courses\/use-data-open-source-python\/intro-raster-data-python\/raster-data-processing\/subtract-rasters-in-python\/\">tutorial by University of Colorado Boulder here<\/a>).<\/p>\n\n\n\n<p>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!).<\/p>\n\n\n\n<p>In this article, I summarize 4 ways to find out and set the CRS in a xarray dataset.<br><em>Disclaimer<\/em>: 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!<\/p>\n\n\n\n<p>To start off, I import libraries as follows.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Import library\n import rioxarray as rio\n import xarray as xr\n from osgeo import gdal\n from pyproj import CRS\n \n \n <\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Import library<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> rioxarray <\/span><span style=\"color: #F47067\">as<\/span><span style=\"color: #ADBAC7\"> rio<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> xarray <\/span><span style=\"color: #F47067\">as<\/span><span style=\"color: #ADBAC7\"> xr<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> osgeo <\/span><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> gdal<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> pyproj <\/span><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">CRS<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> <\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1. Get CRS info with gdal (the easiest option)<\/h2>\n\n\n\n<p>If the data is well-prepared for geo-processing, you will not need to dig into the data file. Gdal will do the job.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Open data with Gdal and get the CRS information\nds0 = gdal.Open(file_path)\ncrs = CRS.from_string(ds0.GetProjection())\n\n# Open data with xarray and set the CRS information obtained above\nds = xr.open_dataset(file_path)\nds.rio.write_crs(crs.to_string(), inplace = True)\n\n\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Open data with Gdal and get the CRS information<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds0 <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> gdal.Open(file_path)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">crs <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">CRS<\/span><span style=\"color: #ADBAC7\">.from_string(ds0.GetProjection())<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Open data with xarray and set the CRS information obtained above<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.open_dataset(file_path)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds.rio.write_crs(crs.to_string(), <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2. Read a wkt or proj4 string from a file<\/h2>\n\n\n\n<p>If the Method 1 does not work, you have to dig into the data file and find out where the CRS information is located.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Open data with xarray\nds = xr.open_dataset(file_path)\n\n# Locate CRS info in the file manually\ncc = ds.TEMP.attrs.crs\n\n# Set the CRS information obtained above\nds.rio.write_crs(cc.to_string(), inplace = True)\n\n\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Open data with xarray<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.open_dataset(file_path)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Locate CRS info in the file manually<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">cc <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> ds.<\/span><span style=\"color: #6CB6FF\">TEMP<\/span><span style=\"color: #ADBAC7\">.attrs.crs<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Set the CRS information obtained above<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds.rio.write_crs(cc.to_string(), <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 3. Read CF from a file, and convert it to CRS<\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/pyproj4.github.io\/pyproj\/dev\/build_crs_cf.html\" target=\"_blank\" rel=\"noreferrer noopener\">pyproj<\/a> website).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Open data with xarray\nds = xr.open_dataset(file_path)\n\n# Locate CRS info in CF convention in the variable file, convert it to CRS format, and read the string as cc\ncc = CRS.from_cf(ds.crs.attrs)\n\n# Set the CRS information obtained above\nds.rio.write_crs(cc.to_string(), inplace = True)\n\n\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Open data with xarray<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.open_dataset(file_path)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Locate CRS info in CF convention in the variable file, convert it to CRS format, and read the string as cc<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">cc <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">CRS<\/span><span style=\"color: #ADBAC7\">.from_cf(ds.crs.attrs)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Set the CRS information obtained above<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds.rio.write_crs(cc.to_string(), <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 4. Write a wkt or proj4 string<\/h2>\n\n\n\n<p id=\"0a31\">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.<\/p>\n\n\n\n<p id=\"7836\">Here are some useful links for this method.<br>To find out the compatibility of different parameter names for common projection: <a href=\"https:\/\/cfconventions.org\/wkt-proj-4.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/cfconventions.org\/wkt-proj-4.html<\/a><br>To find out the formatting template for wkt or proj4 strings (you will only need to tweak the parameters): <a href=\"http:\/\/epsg.io\/4326\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/epsg.io\/4326<\/a><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Open data with xarray\nds = xr.open_dataset(file_path)\n\n# Create a CRS string\ncrs =\n'GEOGCS[\"HRAP_Sphere\",DATUM[\"\",SPHEROID&#91;\"\",6371200.0,0.0&#93;,PRIMEM&#91;\"Greenwich\",0.0&#93;, PARAMETER&#91;\"false_easting\",1905000&#93;,PARAMETER&#91;\"false_northing\",7620000&#93;, PARAMETER&#91;\"latitude_of_origin\",60&#93;, PARAMETER&#91;\"longitude_of_origin\",-105&#93;, UNIT&#91;\"Degree\",0.0174532925199433&#93;]'\n\n# Set the CRS information obtained above\nds.rio.write_crs(crs, inplace=True)\n\n\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Open data with xarray<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.open_dataset(file_path)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Create a CRS string<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">crs <\/span><span style=\"color: #F47067\">=<\/span><\/span>\n<span class=\"line\"><span style=\"color: #96D0FF\">&#39;GEOGCS[&quot;HRAP_Sphere&quot;,DATUM[&quot;&quot;,SPHEROID&#91;&quot;&quot;,6371200.0,0.0&#93;,PRIMEM&#91;&quot;Greenwich&quot;,0.0&#93;, PARAMETER&#91;&quot;false_easting&quot;,1905000&#93;,PARAMETER&#91;&quot;false_northing&quot;,7620000&#93;, PARAMETER&#91;&quot;latitude_of_origin&quot;,60&#93;, PARAMETER&#91;&quot;longitude_of_origin&quot;,-105&#93;, UNIT&#91;&quot;Degree&quot;,0.0174532925199433&#93;]&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Set the CRS information obtained above<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds.rio.write_crs(crs, <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><br>By the way, specifically for <strong>the WRF-Hydro grid<\/strong>, I wrote as follows (referring to <a href=\"https:\/\/www.pkrc.net\/wrf-lambert.html\" target=\"_blank\" rel=\"noreferrer noopener\">this blogpost<\/a>). WRF-Hydro is national hydrologic model in the U.S., and it uses their own grid definitions.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Open data with xarray\nds0 = xr.open_dataset(file_path)\n\n# Create a CRS string\ncrs = '+proj=lcc +lat_1=39 +lat_2=50 +lat_0=39 +lon_0=-135 +a=6370 +b=6370 +towgs84=0,0,0 +no_defs'\n\n# Set the CRS information obtained above\nds0.rio.write_crs(crs1, inplace=True)\n\n# Re-create an xarray object\nds = xr.DataArray(\n     data= ds0.values,\n     dims=&#91;'y', 'x'&#93;,\n     coords={\n         'y': ds0.y.values,\n         'x': ds0.x.values\n     },\n     attrs={'_FillValue': np.nan}\n )\n\n# Re-project the xarray to WGS1984\nds.rio.write_crs(\"EPSG:4326\", inplace=True)\n\n\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #768390\"># Open data with xarray<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds0 <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.open_dataset(file_path)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Create a CRS string<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">crs <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;+proj=lcc +lat_1=39 +lat_2=50 +lat_0=39 +lon_0=-135 +a=6370 +b=6370 +towgs84=0,0,0 +no_defs&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Set the CRS information obtained above<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds0.rio.write_crs(crs1, <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Re-create an xarray object<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> xr.DataArray(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #F69D50\">data<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> ds0.values,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #F69D50\">dims<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\">&#91;<\/span><span style=\"color: #96D0FF\">&#39;y&#39;<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #96D0FF\">&#39;x&#39;<\/span><span style=\"color: #ADBAC7\">&#93;,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #F69D50\">coords<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">         <\/span><span style=\"color: #96D0FF\">&#39;y&#39;<\/span><span style=\"color: #ADBAC7\">: ds0.y.values,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">         <\/span><span style=\"color: #96D0FF\">&#39;x&#39;<\/span><span style=\"color: #ADBAC7\">: ds0.x.values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #F69D50\">attrs<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\">{<\/span><span style=\"color: #96D0FF\">&#39;_FillValue&#39;<\/span><span style=\"color: #ADBAC7\">: np.nan}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\"> )<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #768390\"># Re-project the xarray to WGS1984<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">ds.rio.write_crs(<\/span><span style=\"color: #96D0FF\">&quot;EPSG:4326&quot;<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #F69D50\">inplace<\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #6CB6FF\">True<\/span><span style=\"color: #ADBAC7\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#22272e;color:#9eadbd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>Hope it helps!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":586,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,7],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-earth-science-data","category-python"],"_links":{"self":[{"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":9,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":1116,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions\/1116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/media\/586"}],"wp:attachment":[{"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rarakihydro.com\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}