Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract coordinate values in xarray

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.

like image 213
Ray Bell Avatar asked Apr 27 '17 20:04

Ray Bell


2 Answers

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

like image 95
shoyer Avatar answered Sep 20 '22 20:09

shoyer


Another possible solution is:

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

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

like image 26
till Kadabra Avatar answered Sep 19 '22 20:09

till Kadabra