1

I generated new data and save first as dataset (.to_dataset).

U_reg = U_reg.to_dataset(name = 'U')
V_reg = V_reg.to_dataset(name = 'V')
T_reg = T_reg.to_dataset(name = 'T')

Then I created attributes for each variable

U_reg.attrs['long_name'] = 'eastward_wind'
V_reg.attrs['long_name'] = 'northward_wind'
T_reg.attrs['long_name'] = 'Air Temperature'

Then merged the data as :

new_data= xr.merge([U_reg,V_reg,T_reg]) 

For finally save as a netCDF:

new_data.to_netCDF('new_data.netcdf')

Nevertheless when I open and look at each variable, the attributes are not available.

Is there other way to assign new attributes and have them I the new netCDF generated?

Thanks in advance.

1 Answer 1

0

In your example, you were adding the long_name attribute to the dataset attributes, rather than the attributes for the individual arrays. If you add the attributes before you convert to a dataset (or directly to the dataset variables), you should get your expected output:

U_reg = xr.DataArray(np.random.random((5, 6, 7)), dims=("lon", "lat", "time"))
V_reg = xr.DataArray(np.random.random((5, 6, 7)), dims=("lon", "lat", "time"))
T_reg = xr.DataArray(np.random.random((5, 6, 7)), dims=("lon", "lat", "time"))

U_reg.attrs['long_name'] = 'eastward_wind'
V_reg.attrs['long_name'] = 'northward_wind'
T_reg.attrs['long_name'] = 'Air Temperature'

ds = xr.Dataset({
    "U": U_reg,
    "V": V_reg,
    "T": T_reg
})

ds.info()
xarray.Dataset {
dimensions:
    lon = 5 ;
    lat = 6 ;
    time = 7 ;

variables:
    float64 U(lon, lat, time) ;
        U:long_name = eastward_wind ;
    float64 V(lon, lat, time) ;
        V:long_name = northward_wind ;
    float64 T(lon, lat, time) ;
        T:long_name = Air Temperature ;

// global attributes:
Sign up to request clarification or add additional context in comments.

Comments

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.