2

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I thought this had solved the problem but unfortunately I find that if I then try to save the xarray as a netcdf file and read it back in, the attributes have disappeared. I don't have this problem if I read in a netcdf file and then immediately save it again, only if I go through the process of extracting a single variable and then adding the attributes. Do you know how to solve this issue? I have looked at the possible arguments for to_netcdf and found nothing that seems useful.
You would need to create a Dataset with your DataArray inside. Then add the attrs to the Dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.