0

I am trying to save some variables as a netCDF file. I am taking these steps to do it.

   plev =  xr.DataArray(plev,coords=[levels,lat,lon], dims=["lev","lat","lon"])

    U_v32 = xr.DataArray(U_v32,coords=[levels,lat,lon], dims=["lev","lat","lon"]), 
    attrs=dict(long_name = "eastward_wind" , standard_name = "eastward_wind" , units = "m s-1" , 
        valid_range = "-1.e+15f, 1.e+15f" , vmax = "1.e+15f" , vmin = "-1.e+15f" , coordinates = "lon lat" ,)

Where plev is:

<xarray.DataArray (lev: 32, lat: 192, lon: 288)>
array([[[3.64346569e+00, 3.64346569e+00, 3.64346569e+00, ...,

Coordinates:
  * lev      (lev) int64 1 2 3 4 5 6 7 8 9 10 ... 23 24 25 26 27 28 29 30 31 32
  * lat      (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 87.17 88.11 89.06 90.0
  * lon      (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8

And U_v32 is:

(<xarray.DataArray (lev: 32, lat: 192, lon: 288)>
array([[[-1.12275551e-01, -1.67688605e-01, -2.23421148e-01, ...,
          7.19201197e-02,  6.93718333e-03, -5.42723485e-02],
        [ 4.28133062e+00,  4.01246378e+00,  3.74175282e+00, ...,
          5.00617035e+00,  4.76159409e+00,  4.51793771e+00],
        [ 6.71847498e+00,  6.23453015e+00,  5.75207444e+00, ...,
          8.12475620e+00,  7.66082277e+00,  7.19229493e+00],
        ...,

Coordinates:
  * lev      (lev) int64 1 2 3 4 5 6 7 8 9 10 ... 23 24 25 26 27 28 29 30 31 32
  * lat      (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 87.17 88.11 89.06 90.0
  * lon      (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8,)

Then I do:

    plev = plev.to_dataset(name="plev")
    U_v32 = U_v32.to_dataset(name = 'U')

And finally merge those and then save as netCDF:

    data_v32 = xr.merge([U_v32,plev]) 

    data_v32.to_netcdf(data_v32_final.nc4)

But I got this error before the variables should be merged:

    U_v32 = U_v32.to_dataset(name = 'U')
AttributeError: 'tuple' object has no attribute 'to_dataset'

Is because I defined the attributes for that variable as a dictionary?. Is there a way to not have this error?.

Thanks in advance

1 Answer 1

0

I looked closely when defined the dataArray and I found what was the problem, basically I had to include the attrs inside the xr.DataArray. It was defined out of it:

   U_v32 = xr.DataArray(U_v32,coords=[levels,lat,lon], dims=["lev","lat","lon"], 
    attrs=dict(long_name = "eastward_wind" , standard_name = "eastward_wind" , units = "m s-1" , 
        valid_range = "-1.e+15f, 1.e+15f" , vmax = "1.e+15f" , vmin = "-1.e+15f" , coordinates = "lon lat" ,))

Then the conversion to dataset and merging work fine and I can save the variables as netCDF

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.