0

I'm working with NOAA temperature NetCDF files and trying limit area of interest to the US area only using bounding box. In Jupyter Notebook I wrote few lines to do it and on the plot everything looks good until I load *.nc file to ArcGIS Pro / QGIS, raster is streched along y-axis...

import os, sys
import xarray as xr
from netCDF4 import Dataset
import pandas as pd
import numpy as np
from datetime import date
import rioxarray
import matplotlib.pyplot as plt
%matplotlib inline

ds = xr.open_dataarray(r"C:\Users\test\Downloads\tmax.2022.nc")
new = ds.rio.write_crs("epsg:4326", inplace=True)
new.coords['lon'] = (new.coords['lon'] + 180) % 360 -180

lats = [72, 24]
lons = [-65, -172]
out = new.sel(lat=slice(*lats), lon=slice(*lons))
out.to_netcdf(path_to_output)

enter image description here

1 Answer 1

0

Since you're changing and slicing the coordinates, I think (didn't test it) you'll need to update the transform before saving it (more info here):

out = out.rio.write_transform(out.rio.transform(recalc=True))

Also, you spatial dimensions seemed to be named incorrectly for rioxarray (see here for more info), so you might want to do something like this:

out.rio.set_spatial_dims(x_dim="lon", y_dim="lat" inplace=True)
out.rio.write_coordinate_system(inplace=True)

Notice that when you use inplace=True, you don't need to assign to a new variable like you are doing.

1
  • Thanks a lot for explanation! I reprojected my file after slicing.
    – nomad_gis
    Sep 21, 2022 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.