Skip to content

Commit

Permalink
_RegridTargetPrep(): Handle existing lon/lat dims/variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsrabin committed Aug 28, 2024
1 parent 1e38cbc commit 6d97218
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/landusedata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def _RegridTargetPrep(regrid_target):
by xesmf. As such, this function renames the dimensions. It also adds
lat/lon coordinates based on the LONGXY and LATIXY variables.
"""

# Skip if lat and lon are already the dimension names
if "lat" in regrid_target.dims and "lon" in regrid_target.dims:
return regrid_target

# Drop dimension variables if they already exist
regrid_target = regrid_target.drop_vars("lat", errors="ignore")
regrid_target = regrid_target.drop_vars("lon", errors="ignore")

# Rename dimensions and add coordinates
regrid_target = regrid_target.rename_dims(dims_dict={'lsmlat':'lat','lsmlon':'lon'})
regrid_target['lon'] = regrid_target.LONGXY.isel(lat=0)
regrid_target['lat'] = regrid_target.LATIXY.isel(lon=0)
Expand Down

0 comments on commit 6d97218

Please sign in to comment.