简体   繁体   中英

Save np.array to netCDF4 files with Python

I am trying to save np.array outputs to netCDF4 format with Python. For example, the 2 dimensions are: longitude = [0,25,50,75], latitude = [0,15,30,45] . The variable is: Data = np.arange(16).reshape(4,4) How can I save them into a netCDF4 file, please? Thanks. The file should be like this when opened by xarray:

<xarray.DataArray 'Data' (lat: 4, lon: 4)>
[16 values with dtype=float32]
Coordinates:
  * lon      (lon) float32 0,25,50,75
  * lat      (lat) float32 0,15,30,45

You can simply build the dataarray and save it with xarray to_netcdf() :

df = xr.DataArray(Data, coords=[('lon', longitude), ('lat', latitude)])
df.to_netcdf('filename.nc')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM