Stream: python-questions

Topic: best way to save xarray DataArray to netcdf


view this post on Zulip Mira Berdahl (Dec 13 2022 at 20:56):

Hi,

I have an xarray DataArray that has lat/lon coordinates (see screenshot attached). What is the best way to save this as a netcdf file such that later I can open and plot it properly (ie on different projections etc).

[Screen-Shot-2022-12-13-at-12.48.56-PM.png](user_uploads/2/89/LAmVXBWzPqRTOGF135JMhhXr/Screen-Shot-2022-12-13-at-12.48.56-PM.png)

At the moment I save like this, but lat/lon information doesn't come along . I also can't seem to find the write way to use 'encoding' to save to a specific variable name. What is the correct way to do this?

Clim_AirT_127Control.to_netcdf('/glade/scratch/mberdahl/JupyterHubOutput/atm/temp/2mAirTemp_127Control.nc', mode='w')

I try adding the encoding option but it throws an error:

diff.to_netcdf('/glade/scratch/mberdahl/JupyterHubOutput/atm/temp/diff.nc', mode='w', encoding={'diff':{'dtype':'float32'}})

view this post on Zulip Mira Berdahl (Dec 13 2022 at 21:07):

Screen-Shot-2022-12-13-at-12.48.56-PM.png

view this post on Zulip Brian Bonnlander (Dec 14 2022 at 21:31):

If I'm not mistaken, an xarray Dataset is the data structure that most closely matches what a NetCDF file has in it. A DataArray is more like one variable inside a NetCDF file, minus the coordinates that are normally found there.

https://docs.xarray.dev/en/stable/generated/xarray.Dataset.html

view this post on Zulip Brian Bonnlander (Dec 14 2022 at 22:01):

So I suppose you would assign the DataArray to a Dataset object, and assign coordinates to that Dataset object, before saving to NetCDF. See the "How do I..." section of the xarray documentation for examples of how to do these things.

view this post on Zulip Drew Camron (Dec 14 2022 at 22:08):

Writing DataArrays to netcdf isn't directly possible. DataArray.to_netcdf() instead does this conversion to a Dataset, makes some name assumptions, then writes to netcdf @Brian Bonnlander. @Mira Berdahl your lats and lons should've been written to the resulting file as coordinates. Can you share the output of ncdump -h 2mAirTemp_127Control.nc from the command line?

Separately, to specify encoding, you'll have to make sure the Variable name is the same as your dict key. You don't specify a new name when encoding, instead you can use name=<str> on DataArray creation or modify an existing DataArray with da.name = <str>. Please share the error you get there if that doesn't seem the likely issue!

view this post on Zulip Mira Berdahl (Dec 15 2022 at 20:51):

OK, I think @Drew Camron figured out what I was missing. The lat/lon are indeed written to the resulting file as coordinates, but before I write to netcdf I use da.name = <str> to define the variable name. This way I don't even have to use 'encoding' unless I want to change something about the way that variable is saved (like it's type). Works now - thanks!


Last updated: May 16 2025 at 17:14 UTC