Pad
Pad

Reputation: 911

Assign new coordinates to a variable using xarray

I have a .nc file opened as a dataset in xarray with the following structure:

ds

<xarray.Dataset>
Dimensions:    (lat: 733, lon: 720, time: 204)
Coordinates:
  * time       (time) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Dimensions without coordinates: lat , lon
Data variables :
    latitude   (lat) float32 56.0 55.9917 55.9833 55.975 55.9667 55.9583 ...
    longitude  (lon) float32 -11.0 -10.9917 -10.9833 -10.975 -10.9667 ...
    n2o        (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
    co2        (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
    no3        (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
    ch4        (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
    soc        (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...

There is an issue with this file, the variables (n2o, co2 etc.) have incorrect coordinates, the lat and lon associated with them are wrong. I would like to assign the 'latitude' and 'longitude' data variables (which contain the correct values) as the coordinates of the other variables (n2o, co2 etc.), to replace 'lat' and 'lon'. I am not sure if this is possible.

The 'latitude' variable looks like this:

<xarray.Variable (lat: 733)>
array([ 56.      ,  55.991665,  55.98333 , ...,  49.915367,  49.907032,
    49.898697], dtype=float32)
Attributes:
    units:      degrees of latitude North to South in 30 arc seconds steps
    long_name:  latitude

The ds.co2.lat dimension looks like this:

<xarray.DataArray 'lat' (lat: 733)>
array([  0,   1,   2, ..., 730, 731, 732], dtype=int64)
Dimensions without coordinates: lat

I have tried:

newlat = ds.variables['latitude'][:]

ds.co2['latitude'] = newlat

This didn't seem to do anything - I am not sure what to do next.

Upvotes: 2

Views: 9078

Answers (1)

jhamman
jhamman

Reputation: 6464

You are likely looking for the rename and set_coords methods. What you are seeing is that xarray is not able to determine that latitude/longitude are meant to be coordinate variables. The solution is to first rename the latitude/longitude variables then to associate those variables as coordinates.

In [10]: ds = xr.Dataset({'a': xr.Variable(('lon', 'lat'), np.random.random((4, 5))), 'b': xr.Variable(('lon', 'lat'),np.random.random((4, 5))), 'longitude':
    ...:  xr.Variable('lon', [1, 2, 3, 4]), 'latitude': xr.Variable('lat', [1, 2, 3, 4, 5])})

In [11]: ds
Out[11]:
<xarray.Dataset>
Dimensions:    (lat: 5, lon: 4)
Dimensions without coordinates: lat, lon
Data variables:
    a          (lon, lat) float64 0.8694 0.5929 0.04661 0.9571 0.1814 ...
    b          (lon, lat) float64 0.8893 0.5772 0.8457 0.8337 0.7966 0.1619 ...
    longitude  (lon) int64 1 2 3 4
    latitude   (lat) int64 1 2 3 4 5

In [12]: ds.rename({'latitude': 'lat', 'longitude': 'lon'}).set_coords(['lon', 'lat'])
Out[12]:
<xarray.Dataset>
Dimensions:  (lat: 5, lon: 4)
Coordinates:
  * lon      (lon) int64 1 2 3 4
  * lat      (lat) int64 1 2 3 4 5
Data variables:
    a        (lon, lat) float64 0.8694 0.5929 0.04661 0.9571 0.1814 0.01788 ...
    b        (lon, lat) float64 0.8893 0.5772 0.8457 0.8337 0.7966 0.1619 ...

Upvotes: 7

Related Questions