Reputation: 351
I have a large netcdf file with many dimensions and attributes. I want to extract a single variable from this file and save this as a new netcdf file, while keeping all of the original metadata. I am using xarray
.
I open the dataset using:
dr=xr.open_dataset("path_to_file")
And it looks like this when I print
it (some dimensions and metadata removed for simplicity:
<xarray.Dataset>
Dimensions: (Time: 464, bottom_top: 39, bottom_top_stag: 40, snow_layers_stag: 3, snso_layers_stag: 7, soil_layers_stag: 4, south_north: 186, south_north_stag: 187, west_east: 246, west_east_stag: 247)
Coordinates:
XLAT (Time, south_north, west_east) float32 ...
XLONG (Time, south_north, west_east) float32 ...
XTIME (Time) datetime64[ns] ...
XLAT_U (Time, south_north, west_east_stag) float32 ...
XLONG_U (Time, south_north, west_east_stag) float32 ...
XLAT_V (Time, south_north_stag, west_east) float32 ...
XLONG_V (Time, south_north_stag, west_east) float32 ...
Dimensions without coordinates: Time, bottom_top, bottom_top_stag, snow_layers_stag, snso_layers_stag, soil_layers_stag, south_north, south_north_stag, west_east, west_east_stag
Data variables:
Times (Time) |S19 ...
UST (Time, south_north, west_east) float32 ...
ZNU (Time, bottom_top) float32 ...
ZNW (Time, bottom_top_stag) float32 ...
ZS (Time, soil_layers_stag) float32 ...
DZS (Time, soil_layers_stag) float32 ...
Attributes:
TITLE: OUTPUT FROM WRF V3.9 MODEL
START_DATE: 2017-10-31_00:00:00
SIMULATION_START_DATE: 2017-10-01_00:00:00
WEST-EAST_GRID_DIMENSION: 247
SOUTH-NORTH_GRID_DIMENSION: 187
BOTTOM-TOP_GRID_DIMENSION: 40
HYBRID_OPT: -1
ETAC: 0.0
I want to extract just UST
, so I try:
dr_u = dr['UST']
But when I print
the resulting dr_u
, the metadata is gone:
<xarray.Dataset>
Dimensions: (Time: 464, south_north: 186, west_east: 246)
Coordinates:
XLAT (Time, south_north, west_east) float32 ...
XLONG (Time, south_north, west_east) float32 ...
XTIME (Time) datetime64[ns] ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
UST (Time, south_north, west_east) float32 ...
I want to be able to keep all of the information under the Attributes
heading in the original file. I'm aware there is a flag called keep_attrs
in the xarray
package that seems like it would be useful for this, but I can't work out how to use it on this operation.
Upvotes: 2
Views: 1699
Reputation: 1115
You can retrieve the attribute dictionary from an xarray object using ds.attrs
You can just assign the attributes manually:
dr_u.attrs = dr.attrs
Upvotes: 1