Last active
February 17, 2024 22:58
-
-
Save bennyistanto/06f2909751eedaf0f504b6247acb5650 to your computer and use it in GitHub Desktop.
Rename variable, dimension and unit in a netcdf file for climate-indices calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Rename variable, dimension and unit in a netcdf file for climate-indices calculation | |
# Source: https://github.com/monocongo/climate_indices/issues/404#issuecomment-735474320 | |
# https://stackoverflow.com/a/55686749 | |
import pandas as pd | |
import xarray as xr | |
ds = xr.open_dataset("/Users/bennyistanto/Temp/CHIRTS/nc/rbb_cli_chirts_monthly_tmax_1983_2016_d1.nc", | |
decode_times=False) | |
units, reference_date = ds.time.attrs['units'].split('since') | |
ds['time'] = pd.date_range(start=reference_date, periods=ds.sizes['time'], freq='MS') | |
ds["Tmax"].attrs["units"] = "degrees_celsius" | |
name_dict = { | |
"latitude": "lat", | |
"longitude": "lon", | |
} | |
ds = ds.rename(name_dict=name_dict) | |
ds.to_netcdf(path="/Users/bennyistanto/Temp/CHIRTS/nc/rbb_cli_chirts_monthly_tmax_1983_2016_d1a.nc") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment