23

I would like to extract the values of the coordinate variables.

For example I create a DataArray as:

import xarray as xr
import numpy as np
import pandas as pd
years_arr=range(1982,1986)
time = pd.date_range('14/1/' + str(years_arr[0]) + ' 12:00:00', periods=len(years_arr), freq=pd.DateOffset(years=1))
lon = range(20,24)
lat = range(10,14)
arr1 = xr.DataArray(data, coords=[time, lat, lon], dims=['time', 'latitude', 'longitude'])

I now would like to output the lon values from arr1. I'm asking as arr1 going into a function so I may not have the lon values available.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Ray Bell
  • 1,508
  • 4
  • 18
  • 45

2 Answers2

28

arr1.coords['lon'] gives you longitude as a xarray.DataArray, and arr1.coords['lon'].values gives you the values as a numpy array.

shoyer
  • 9,165
  • 1
  • 37
  • 55
6

Another possible solution is:

time, lat, lon = arr1.indexes.values()

The result is a Float64Index for your lat/lon coordinates.

till Kadabra
  • 478
  • 9
  • 21