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.
arr1.coords['lon']
gives you longitude as a xarray.DataArray
, and arr1.coords['lon'].values
gives you the values as a numpy array.
Another possible solution is:
time, lat, lon = arr1.indexes.values()
The result is a Float64Index for your lat/lon coordinates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With