Skip to content

Instantly share code, notes, and snippets.

@nmathewa
Created May 19, 2021 12:44
Show Gist options
  • Save nmathewa/61522659bc21c5406129b80d2449ef54 to your computer and use it in GitHub Desktop.
Save nmathewa/61522659bc21c5406129b80d2449ef54 to your computer and use it in GitHub Desktop.
netcdf using xarray
"""
Created on Wed May 19 17:55:14 2021
@author: mathew
"""
import xarray as xr
import numpy as np
# making a random spatial dataset
# 12 time-step and 30x30 grid points
vals = np.random.rand(12,30,30)
time = np.arange(12)
lats = np.linspace(10,20,30)
lon = np.linspace(70,80,30)
# creating xarray dataset
dset = xr.Dataset({
"values" : (["time","lon","lat"],vals),},
coords = {"time":(["time",],time),
"lon":(["lon",],lon),
"lat":(["lat",],lats)})
dset.to_netcdf("file.nc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment