Change coordinate in file DFSU to NetCDF #842
minhmminh0419-lgtm
started this conversation in
General
Replies: 1 comment
-
@daniel-caichac-DHI could you guide @minhmminh0419-lgtm a bit here? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a DFSU file whose coordinates are in WGS84 / UTM Zone 48N, but I don’t know how to convert them to longitude and latitude. I tried using PyProj in Python but it didn’t work. Could you please offer some advice? Thanks in advance!
Here is my code:
import xarray as xr
import numpy as np
from pyproj import Transformer
ds = xr.open_dataset("tide_u.nc")
x = ds["x"].values # shape (nx,)
y = ds["y"].values # shape (ny,)
if x.ndim == 1 and y.ndim == 1:
xx, yy = np.meshgrid(x, y) # (ny, nx)
else:
xx, yy = x, y
transformer = Transformer.from_crs("EPSG:32648", "EPSG:4326", always_xy=True)
lon2d, lat2d = transformer.transform(xx, yy)
ds = ds.assign_coords({
"lon": (("y","x"), lon2d),
"lat": (("y","x"), lat2d)
})
ds = ds.rename_dims({"x": "longitude", "y": "latitude"})
ds = ds.assign_coords({
"latitude": (("latitude", "longitude"), lat2d),
"longitude": (("latitude", "longitude"), lon2d),
})
outfile = "u_lonlat.nc"
ds.to_netcdf(outfile)
Beta Was this translation helpful? Give feedback.
All reactions