XArray Introduction
XArray¶
XArray expands on the capabilities on NumPy arrays, providing a lot of streamlined data manipulation. It is similar in that respect to Pandas, but whereas Pandas excels at working with tabular data, XArray is focused on N-dimensional arrays of data (i.e. grids). Its interface is based largely on the netCDF data model (variables, attributes, and dimensions), but it goes beyond the traditional netCDF interfaces to provide functionality similar to netCDF-java's Common Data Model (CDM).
DataArray
¶
The DataArray
is one of the basic building blocks of XArray. It provides a NumPy ndarray-like object that expands to provide two critical pieces of functionality:
- Coordinate names and values are stored with the data, making slicing and indexing much more powerful
- It has a built-in container for attributes
# Convention for import to get shortened namespace
import numpy as np
import xarray as xr
# Create some sample "temperature" data
data = 283 + 5 * np.random.randn(5, 3, 4)
data
array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]])
Here we create a basic DataArray
by passing it just a numpy array of random data. Note that XArray generates some basic dimension names for us.
temp = xr.DataArray(data)
temp
<xarray.DataArray (dim_0: 5, dim_1: 3, dim_2: 4)> array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]]) Dimensions without coordinates: dim_0, dim_1, dim_2
- dim_0: 5
- dim_1: 3
- dim_2: 4
- 288.7 282.9 289.4 289.3 285.8 282.8 ... 291.0 265.5 286.6 282.7 282.4
array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]])
-
-
We can also pass in our own dimension names:
temp = xr.DataArray(data, dims=['time', 'lat', 'lon'])
temp
<xarray.DataArray (time: 5, lat: 3, lon: 4)> array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]]) Dimensions without coordinates: time, lat, lon
- time: 5
- lat: 3
- lon: 4
- 288.7 282.9 289.4 289.3 285.8 282.8 ... 291.0 265.5 286.6 282.7 282.4
array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]])
-
-
This is already improved upon from a numpy array, because we have names for each of the dimensions (or axes in NumPy parlance). Even better, we can take arrays representing the values for the coordinates for each of these dimensions and associate them with the data when we create the DataArray
.
# Use pandas to create an array of datetimes
import pandas as pd
times = pd.date_range('2018-01-01', periods=5)
times
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05'], dtype='datetime64[ns]', freq='D')
# Sample lon/lats
lons = np.linspace(-120, -60, 4)
lats = np.linspace(25, 55, 3)
When we create the DataArray
instance, we pass in the arrays we just created:
temp = xr.DataArray(data, coords=[times, lats, lons], dims=['time', 'lat', 'lon'])
temp
<xarray.DataArray (time: 5, lat: 3, lon: 4)> array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 ... 2018-01-05 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0
- time: 5
- lat: 3
- lon: 4
- 288.7 282.9 289.4 289.3 285.8 282.8 ... 291.0 265.5 286.6 282.7 282.4
array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]])
-
-
time(time)datetime64[ns]2018-01-01 ... 2018-01-05
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000', '2018-01-04T00:00:00.000000000', '2018-01-05T00:00:00.000000000'], dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
...and we can also set some attribute metadata:
temp.attrs['units'] = 'kelvin'
temp.attrs['standard_name'] = 'air_temperature'
temp
<xarray.DataArray (time: 5, lat: 3, lon: 4)> array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 ... 2018-01-05 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0 Attributes: units: kelvin standard_name: air_temperature
- time: 5
- lat: 3
- lon: 4
- 288.7 282.9 289.4 289.3 285.8 282.8 ... 291.0 265.5 286.6 282.7 282.4
array([[[288.6717353 , 282.86812983, 289.37201191, 289.31986918], [285.83487764, 282.77615342, 282.93472659, 283.23135707], [277.945099 , 285.43115054, 287.77345197, 276.61585094]], [[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]], [[280.28475597, 277.00294807, 281.70299506, 285.02187614], [283.3136319 , 285.10185196, 288.07733264, 286.67059564], [280.54805382, 284.38031707, 279.94037853, 286.85580016]], [[271.38161375, 278.71340601, 279.83642271, 284.36803707], [280.95591716, 283.94325187, 283.7348996 , 286.52561326], [287.3256393 , 286.39359029, 284.75952091, 276.56980633]], [[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]])
-
-
time(time)datetime64[ns]2018-01-01 ... 2018-01-05
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000', '2018-01-04T00:00:00.000000000', '2018-01-05T00:00:00.000000000'], dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
Notice what happens if we perform a mathematical operaton with the DataArray
: the coordinate values persist, but the attributes are lost. This is done because it is very challenging to know if the attribute metadata is still correct or appropriate after arbitrary arithmetic operations.
# For example, convert Kelvin to Celsius
temp - 273.15
<xarray.DataArray (time: 5, lat: 3, lon: 4)> array([[[15.5217353 , 9.71812983, 16.22201191, 16.16986918], [12.68487764, 9.62615342, 9.78472659, 10.08135707], [ 4.795099 , 12.28115054, 14.62345197, 3.46585094]], [[ 9.6483004 , 6.86172407, 7.95145953, 7.77659817], [14.11062971, 16.0118384 , 9.32171833, 13.05171911], [14.9863946 , 15.13544086, 13.92000053, 5.30082951]], [[ 7.13475597, 3.85294807, 8.55299506, 11.87187614], [10.1636319 , 11.95185196, 14.92733264, 13.52059564], [ 7.39805382, 11.23031707, 6.79037853, 13.70580016]], [[-1.76838625, 5.56340601, 6.68642271, 11.21803707], [ 7.80591716, 10.79325187, 10.5848996 , 13.37561326], [14.1756393 , 13.24359029, 11.60952091, 3.41980633]], [[10.53426701, 7.20584331, 3.2188505 , 15.67948095], [ 9.40974942, 11.17670363, 16.61757034, 17.89432312], [-7.65165521, 13.44971571, 9.57050244, 9.29922247]]]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 ... 2018-01-05 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0
- time: 5
- lat: 3
- lon: 4
- 15.52 9.718 16.22 16.17 12.68 9.626 ... 17.89 -7.652 13.45 9.571 9.299
array([[[15.5217353 , 9.71812983, 16.22201191, 16.16986918], [12.68487764, 9.62615342, 9.78472659, 10.08135707], [ 4.795099 , 12.28115054, 14.62345197, 3.46585094]], [[ 9.6483004 , 6.86172407, 7.95145953, 7.77659817], [14.11062971, 16.0118384 , 9.32171833, 13.05171911], [14.9863946 , 15.13544086, 13.92000053, 5.30082951]], [[ 7.13475597, 3.85294807, 8.55299506, 11.87187614], [10.1636319 , 11.95185196, 14.92733264, 13.52059564], [ 7.39805382, 11.23031707, 6.79037853, 13.70580016]], [[-1.76838625, 5.56340601, 6.68642271, 11.21803707], [ 7.80591716, 10.79325187, 10.5848996 , 13.37561326], [14.1756393 , 13.24359029, 11.60952091, 3.41980633]], [[10.53426701, 7.20584331, 3.2188505 , 15.67948095], [ 9.40974942, 11.17670363, 16.61757034, 17.89432312], [-7.65165521, 13.44971571, 9.57050244, 9.29922247]]])
-
-
time(time)datetime64[ns]2018-01-01 ... 2018-01-05
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000', '2018-01-04T00:00:00.000000000', '2018-01-05T00:00:00.000000000'], dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
Selection¶
We can use the .sel
method to select portions of our data based on these coordinate values, rather than using indices (this is similar to the CDM).
temp.sel(time='2018-01-02')
<xarray.DataArray (lat: 3, lon: 4)> array([[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]]) Coordinates: time datetime64[ns] 2018-01-02 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0 Attributes: units: kelvin standard_name: air_temperature
- lat: 3
- lon: 4
- 282.8 280.0 281.1 280.9 287.3 289.2 ... 286.2 288.1 288.3 287.1 278.5
array([[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]])
-
-
time()datetime64[ns]2018-01-02
array('2018-01-02T00:00:00.000000000', dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
.sel
has the flexibility to also perform nearest neighbor sampling, taking an optional tolerance:
from datetime import timedelta
temp.sel(time='2018-01-07', method='nearest', tolerance=timedelta(days=2))
<xarray.DataArray (lat: 3, lon: 4)> array([[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]]) Coordinates: time datetime64[ns] 2018-01-05 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0 Attributes: units: kelvin standard_name: air_temperature
- lat: 3
- lon: 4
- 283.7 280.4 276.4 288.8 282.6 284.3 ... 291.0 265.5 286.6 282.7 282.4
array([[283.68426701, 280.35584331, 276.3688505 , 288.82948095], [282.55974942, 284.32670363, 289.76757034, 291.04432312], [265.49834479, 286.59971571, 282.72050244, 282.44922247]])
-
-
time()datetime64[ns]2018-01-05
array('2018-01-05T00:00:00.000000000', dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
# YOUR CODE GOES HERE
# %load solutions/interp_solution.py
# Cell content replaced by load magic replacement.
temp.interp(lon=-105, lat=40)
<xarray.DataArray (time: 5)> array([283.54083448, 288.68653623, 284.65479695, 283.19641819, 283.88496508]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 ... 2018-01-05 lon int64 -105 lat int64 40 Attributes: units: kelvin standard_name: air_temperature
- time: 5
- 283.5 288.7 284.7 283.2 283.9
array([283.54083448, 288.68653623, 284.65479695, 283.19641819, 283.88496508])
-
-
time(time)datetime64[ns]2018-01-01 ... 2018-01-05
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000', '2018-01-04T00:00:00.000000000', '2018-01-05T00:00:00.000000000'], dtype='datetime64[ns]')
-
lon()int64-105
array(-105)
-
lat()int6440
array(40)
-
-
- units :
- kelvin
- standard_name :
- air_temperature
Slicing with Selection¶
temp.sel(time=slice('2018-01-01', '2018-01-03'), lon=slice(-110, -70), lat=slice(25, 45))
<xarray.DataArray (time: 3, lat: 2, lon: 2)> array([[[282.86812983, 289.37201191], [282.77615342, 282.93472659]], [[280.01172407, 281.10145953], [289.1618384 , 282.47171833]], [[277.00294807, 281.70299506], [285.10185196, 288.07733264]]]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 2018-01-03 * lat (lat) float64 25.0 40.0 * lon (lon) float64 -100.0 -80.0 Attributes: units: kelvin standard_name: air_temperature
- time: 3
- lat: 2
- lon: 2
- 282.9 289.4 282.8 282.9 280.0 281.1 ... 282.5 277.0 281.7 285.1 288.1
array([[[282.86812983, 289.37201191], [282.77615342, 282.93472659]], [[280.01172407, 281.10145953], [289.1618384 , 282.47171833]], [[277.00294807, 281.70299506], [285.10185196, 288.07733264]]])
-
-
time(time)datetime64[ns]2018-01-01 2018-01-02 2018-01-03
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000'], dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0
array([25., 40.])
-
lon(lon)float64-100.0 -80.0
array([-100., -80.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
.loc
¶
All of these operations can also be done within square brackets on the .loc
attribute of the DataArray
. This permits a much more numpy-looking syntax, though you lose the ability to specify the names of the various dimensions. Instead, the slicing must be done in the correct order.
# As done above
temp.loc['2018-01-02']
<xarray.DataArray (lat: 3, lon: 4)> array([[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]]) Coordinates: time datetime64[ns] 2018-01-02 * lat (lat) float64 25.0 40.0 55.0 * lon (lon) float64 -120.0 -100.0 -80.0 -60.0 Attributes: units: kelvin standard_name: air_temperature
- lat: 3
- lon: 4
- 282.8 280.0 281.1 280.9 287.3 289.2 ... 286.2 288.1 288.3 287.1 278.5
array([[282.7983004 , 280.01172407, 281.10145953, 280.92659817], [287.26062971, 289.1618384 , 282.47171833, 286.20171911], [288.1363946 , 288.28544086, 287.07000053, 278.45082951]])
-
-
time()datetime64[ns]2018-01-02
array('2018-01-02T00:00:00.000000000', dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0 55.0
array([25., 40., 55.])
-
lon(lon)float64-120.0 -100.0 -80.0 -60.0
array([-120., -100., -80., -60.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
temp.loc['2018-01-01':'2018-01-03', 25:45, -110:-70]
<xarray.DataArray (time: 3, lat: 2, lon: 2)> array([[[282.86812983, 289.37201191], [282.77615342, 282.93472659]], [[280.01172407, 281.10145953], [289.1618384 , 282.47171833]], [[277.00294807, 281.70299506], [285.10185196, 288.07733264]]]) Coordinates: * time (time) datetime64[ns] 2018-01-01 2018-01-02 2018-01-03 * lat (lat) float64 25.0 40.0 * lon (lon) float64 -100.0 -80.0 Attributes: units: kelvin standard_name: air_temperature
- time: 3
- lat: 2
- lon: 2
- 282.9 289.4 282.8 282.9 280.0 281.1 ... 282.5 277.0 281.7 285.1 288.1
array([[[282.86812983, 289.37201191], [282.77615342, 282.93472659]], [[280.01172407, 281.10145953], [289.1618384 , 282.47171833]], [[277.00294807, 281.70299506], [285.10185196, 288.07733264]]])
-
-
time(time)datetime64[ns]2018-01-01 2018-01-02 2018-01-03
array(['2018-01-01T00:00:00.000000000', '2018-01-02T00:00:00.000000000', '2018-01-03T00:00:00.000000000'], dtype='datetime64[ns]')
-
lat(lat)float6425.0 40.0
array([25., 40.])
-
lon(lon)float64-100.0 -80.0
array([-100., -80.])
-
-
- units :
- kelvin
- standard_name :
- air_temperature
This does not work however:
temp.loc[-110:-70, 25:45,'2018-01-01':'2018-01-03']
Opening netCDF data¶
With its close ties to the netCDF data model, XArray also supports netCDF as a first-class file format. This means it has easy support for opening netCDF datasets, so long as they conform to some of XArray's limitations (such as 1-dimensional coordinates).
# Open sample North American Reanalysis data in netCDF format
ds = xr.open_dataset('../../../data/NARR_19930313_0000.nc')
ds
<xarray.Dataset> Dimensions: (isobaric1: 29, time1: 1, x: 268, y: 119) Coordinates: * time1 (time1) datetime64[ns] 1993-03-13 * isobaric1 (isobaric1) float32 100.0 125.0 ... 1000.0 * y (y) float32 -3116.548 -3084.0852 ... 714.08594 * x (x) float32 -3324.4707 ... 5343.1504 Data variables: u-component_of_wind_isobaric (time1, isobaric1, y, x) float32 ... LambertConformal_Projection int32 0 lat (y, x) float64 17.87 17.96 ... 34.85 34.64 lon (y, x) float64 -135.0 -134.8 ... -43.13 -42.91 Geopotential_height_isobaric (time1, isobaric1, y, x) float32 ... v-component_of_wind_isobaric (time1, isobaric1, y, x) float32 ... Temperature_isobaric (time1, isobaric1, y, x) float32 ... Attributes: Originating_or_generating_Center: US National Weather Service, Nation... Originating_or_generating_Subcenter: North American Regional Reanalysis ... GRIB_table_version: 0,131 Generating_process_or_model: North American Regional Reanalysis ... Conventions: CF-1.6 history: Read using CDM IOSP GribCollection v3 featureType: GRID History: Translated to CF-1.0 Conventions by... geospatial_lat_min: 10.753308882144761 geospatial_lat_max: 46.8308828962289 geospatial_lon_min: -153.88242040519995 geospatial_lon_max: -42.666108129242815
-
- isobaric1: 29
- time1: 1
- x: 268
- y: 119
-
-
time1(time1)datetime64[ns]1993-03-13
- standard_name :
- time
- long_name :
- GRIB forecast or observation time
- _CoordinateAxisType :
- Time
array(['1993-03-13T00:00:00.000000000'], dtype='datetime64[ns]')
-
isobaric1(isobaric1)float32100.0 125.0 150.0 ... 975.0 1000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
y(y)float32-3116.548 -3084.0852 ... 714.08594
- standard_name :
- projection_y_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoY
array([-3.116548e+03, -3.084085e+03, -3.051622e+03, -3.019159e+03, -2.986696e+03, -2.954233e+03, -2.921770e+03, -2.889307e+03, -2.856844e+03, -2.824381e+03, -2.791918e+03, -2.759455e+03, -2.726992e+03, -2.694529e+03, -2.662066e+03, -2.629603e+03, -2.597140e+03, -2.564677e+03, -2.532214e+03, -2.499751e+03, -2.467288e+03, -2.434825e+03, -2.402362e+03, -2.369899e+03, -2.337436e+03, -2.304973e+03, -2.272510e+03, -2.240047e+03, -2.207584e+03, -2.175121e+03, -2.142658e+03, -2.110195e+03, -2.077732e+03, -2.045269e+03, -2.012806e+03, -1.980343e+03, -1.947880e+03, -1.915417e+03, -1.882954e+03, -1.850491e+03, -1.818028e+03, -1.785565e+03, -1.753102e+03, -1.720639e+03, -1.688176e+03, -1.655713e+03, -1.623250e+03, -1.590787e+03, -1.558324e+03, -1.525861e+03, -1.493398e+03, -1.460935e+03, -1.428472e+03, -1.396009e+03, -1.363546e+03, -1.331083e+03, -1.298620e+03, -1.266157e+03, -1.233694e+03, -1.201231e+03, -1.168768e+03, -1.136305e+03, -1.103842e+03, -1.071379e+03, -1.038916e+03, -1.006453e+03, -9.739901e+02, -9.415271e+02, -9.090641e+02, -8.766011e+02, -8.441381e+02, -8.116751e+02, -7.792121e+02, -7.467491e+02, -7.142861e+02, -6.818231e+02, -6.493601e+02, -6.168971e+02, -5.844341e+02, -5.519711e+02, -5.195081e+02, -4.870451e+02, -4.545821e+02, -4.221191e+02, -3.896561e+02, -3.571931e+02, -3.247301e+02, -2.922671e+02, -2.598041e+02, -2.273411e+02, -1.948781e+02, -1.624151e+02, -1.299521e+02, -9.748907e+01, -6.502608e+01, -3.256308e+01, -1.000744e-01, 3.236293e+01, 6.482593e+01, 9.728893e+01, 1.297519e+02, 1.622149e+02, 1.946779e+02, 2.271409e+02, 2.596039e+02, 2.920669e+02, 3.245299e+02, 3.569930e+02, 3.894559e+02, 4.219189e+02, 4.543820e+02, 4.868449e+02, 5.193079e+02, 5.517709e+02, 5.842339e+02, 6.166970e+02, 6.491600e+02, 6.816229e+02, 7.140859e+02], dtype=float32)
-
x(x)float32-3324.4707 -3292.0078 ... 5343.1504
- standard_name :
- projection_x_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoX
array([-3324.4707, -3292.0078, -3259.5447, ..., 5278.2246, 5310.6875, 5343.1504], dtype=float32)
-
-
-
u-component_of_wind_isobaric(time1, isobaric1, y, x)float32...
- long_name :
- u-component of wind @ Isobaric surface
- units :
- m s^-1
- description :
- u-component of wind
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-33_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 33
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
[924868 values with dtype=float32]
-
LambertConformal_Projection()int32...
- grid_mapping_name :
- lambert_conformal_conic
- latitude_of_projection_origin :
- 50.000003814697266
- longitude_of_central_meridian :
- -107.00000762939453
- standard_parallel :
- 50.000003814697266
- earth_radius :
- 6367470.0
- _CoordinateTransformType :
- Projection
- _CoordinateAxisTypes :
- GeoX GeoY
array(0, dtype=int32)
-
lat(y, x)float64...
- units :
- degrees_north
- long_name :
- latitude coordinate
- standard_name :
- latitude
- _CoordinateAxisType :
- Lat
array([[17.871157, 17.96379 , 18.055705, ..., 11.171237, 11.04458 , 10.917531], [18.107957, 18.201085, 18.293491, ..., 11.374014, 11.246746, 11.119084], [18.345107, 18.43873 , 18.53163 , ..., 11.57699 , 11.449107, 11.320831], ..., [46.323111, 46.49107 , 46.657954, ..., 34.691863, 34.480463, 34.268685], [46.56076 , 46.72954 , 46.897241, ..., 34.879394, 34.667184, 34.454598], [46.79791 , 46.967512, 47.136034, ..., 35.066338, 34.853317, 34.639922]])
-
lon(y, x)float64...
- units :
- degrees_east
- long_name :
- longitude coordinate
- standard_name :
- longitude
- _CoordinateAxisType :
- Lon
array([[-135.006462, -134.757515, -134.50792 , ..., -65.276918, -65.070682, -64.865157], [-135.104494, -134.854843, -134.604538, ..., -65.14766 , -64.941078, -64.735212], [-135.203181, -134.952825, -134.701807, ..., -65.017683, -64.810756, -64.60455 ], ..., [-153.092434, -152.746829, -152.398966, ..., -43.878114, -43.650466, -43.424375], [-153.337548, -152.991143, -152.642454, ..., -43.620454, -43.393 , -43.167112], [-153.584937, -153.237739, -152.88823 , ..., -43.361216, -43.133969, -42.908296]])
-
Geopotential_height_isobaric(time1, isobaric1, y, x)float32...
- long_name :
- Geopotential height @ Isobaric surface
- units :
- gpm
- description :
- Geopotential height
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-7_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 7
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
[924868 values with dtype=float32]
-
v-component_of_wind_isobaric(time1, isobaric1, y, x)float32...
- long_name :
- v-component of wind @ Isobaric surface
- units :
- m s^-1
- description :
- v-component of wind
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-34_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 34
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
[924868 values with dtype=float32]
-
Temperature_isobaric(time1, isobaric1, y, x)float32...
- long_name :
- Temperature @ Isobaric surface
- units :
- K
- description :
- Temperature
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-11_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 11
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
[924868 values with dtype=float32]
-
-
- Originating_or_generating_Center :
- US National Weather Service, National Centres for Environmental Prediction (NCEP)
- Originating_or_generating_Subcenter :
- North American Regional Reanalysis Project
- GRIB_table_version :
- 0,131
- Generating_process_or_model :
- North American Regional Reanalysis (NARR)
- Conventions :
- CF-1.6
- history :
- Read using CDM IOSP GribCollection v3
- featureType :
- GRID
- History :
- Translated to CF-1.0 Conventions by Netcdf-Java CDM (CFGridWriter2) Original Dataset = DatasetScan#narr-a_221_19930313_0000_000.grb; Translation Date = 2018-01-04T23:57:16.043Z
- geospatial_lat_min :
- 10.753308882144761
- geospatial_lat_max :
- 46.8308828962289
- geospatial_lon_min :
- -153.88242040519995
- geospatial_lon_max :
- -42.666108129242815
This returns a Dataset
object, which is a container that contains one or more DataArray
s, which can also optionally share coordinates. We can then pull out individual fields:
ds.isobaric1
<xarray.DataArray 'isobaric1' (isobaric1: 29)> array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32) Coordinates: * isobaric1 (isobaric1) float32 100.0 125.0 150.0 ... 950.0 975.0 1000.0 Attributes: units: hPa long_name: Isobaric surface positive: down Grib_level_type: 100 _CoordinateAxisType: Pressure _CoordinateZisPositive: down
- isobaric1: 29
- 100.0 125.0 150.0 175.0 200.0 225.0 ... 900.0 925.0 950.0 975.0 1000.0
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
isobaric1(isobaric1)float32100.0 125.0 150.0 ... 975.0 1000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
or
ds['isobaric1']
<xarray.DataArray 'isobaric1' (isobaric1: 29)> array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32) Coordinates: * isobaric1 (isobaric1) float32 100.0 125.0 150.0 ... 950.0 975.0 1000.0 Attributes: units: hPa long_name: Isobaric surface positive: down Grib_level_type: 100 _CoordinateAxisType: Pressure _CoordinateZisPositive: down
- isobaric1: 29
- 100.0 125.0 150.0 175.0 200.0 225.0 ... 900.0 925.0 950.0 975.0 1000.0
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
isobaric1(isobaric1)float32100.0 125.0 150.0 ... 975.0 1000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
Dataset
s also support much of the same subsetting operations as DataArray
, but will perform the operation on all data:
ds_1000 = ds.sel(isobaric1=1000.0)
ds_1000
<xarray.Dataset> Dimensions: (time1: 1, x: 268, y: 119) Coordinates: * time1 (time1) datetime64[ns] 1993-03-13 isobaric1 float32 1000.0 * y (y) float32 -3116.548 -3084.0852 ... 714.08594 * x (x) float32 -3324.4707 ... 5343.1504 Data variables: u-component_of_wind_isobaric (time1, y, x) float32 -5.377701 ... 7.497299 LambertConformal_Projection int32 0 lat (y, x) float64 17.87 17.96 ... 34.85 34.64 lon (y, x) float64 -135.0 -134.8 ... -43.13 -42.91 Geopotential_height_isobaric (time1, y, x) float32 153.24702 ... 203.74702 v-component_of_wind_isobaric (time1, y, x) float32 -5.755432 ... 9.275818 Temperature_isobaric (time1, y, x) float32 294.09436 ... 288.96936 Attributes: Originating_or_generating_Center: US National Weather Service, Nation... Originating_or_generating_Subcenter: North American Regional Reanalysis ... GRIB_table_version: 0,131 Generating_process_or_model: North American Regional Reanalysis ... Conventions: CF-1.6 history: Read using CDM IOSP GribCollection v3 featureType: GRID History: Translated to CF-1.0 Conventions by... geospatial_lat_min: 10.753308882144761 geospatial_lat_max: 46.8308828962289 geospatial_lon_min: -153.88242040519995 geospatial_lon_max: -42.666108129242815
-
- time1: 1
- x: 268
- y: 119
-
-
time1(time1)datetime64[ns]1993-03-13
- standard_name :
- time
- long_name :
- GRIB forecast or observation time
- _CoordinateAxisType :
- Time
array(['1993-03-13T00:00:00.000000000'], dtype='datetime64[ns]')
-
isobaric1()float321000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array(1000., dtype=float32)
-
y(y)float32-3116.548 -3084.0852 ... 714.08594
- standard_name :
- projection_y_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoY
array([-3.116548e+03, -3.084085e+03, -3.051622e+03, -3.019159e+03, -2.986696e+03, -2.954233e+03, -2.921770e+03, -2.889307e+03, -2.856844e+03, -2.824381e+03, -2.791918e+03, -2.759455e+03, -2.726992e+03, -2.694529e+03, -2.662066e+03, -2.629603e+03, -2.597140e+03, -2.564677e+03, -2.532214e+03, -2.499751e+03, -2.467288e+03, -2.434825e+03, -2.402362e+03, -2.369899e+03, -2.337436e+03, -2.304973e+03, -2.272510e+03, -2.240047e+03, -2.207584e+03, -2.175121e+03, -2.142658e+03, -2.110195e+03, -2.077732e+03, -2.045269e+03, -2.012806e+03, -1.980343e+03, -1.947880e+03, -1.915417e+03, -1.882954e+03, -1.850491e+03, -1.818028e+03, -1.785565e+03, -1.753102e+03, -1.720639e+03, -1.688176e+03, -1.655713e+03, -1.623250e+03, -1.590787e+03, -1.558324e+03, -1.525861e+03, -1.493398e+03, -1.460935e+03, -1.428472e+03, -1.396009e+03, -1.363546e+03, -1.331083e+03, -1.298620e+03, -1.266157e+03, -1.233694e+03, -1.201231e+03, -1.168768e+03, -1.136305e+03, -1.103842e+03, -1.071379e+03, -1.038916e+03, -1.006453e+03, -9.739901e+02, -9.415271e+02, -9.090641e+02, -8.766011e+02, -8.441381e+02, -8.116751e+02, -7.792121e+02, -7.467491e+02, -7.142861e+02, -6.818231e+02, -6.493601e+02, -6.168971e+02, -5.844341e+02, -5.519711e+02, -5.195081e+02, -4.870451e+02, -4.545821e+02, -4.221191e+02, -3.896561e+02, -3.571931e+02, -3.247301e+02, -2.922671e+02, -2.598041e+02, -2.273411e+02, -1.948781e+02, -1.624151e+02, -1.299521e+02, -9.748907e+01, -6.502608e+01, -3.256308e+01, -1.000744e-01, 3.236293e+01, 6.482593e+01, 9.728893e+01, 1.297519e+02, 1.622149e+02, 1.946779e+02, 2.271409e+02, 2.596039e+02, 2.920669e+02, 3.245299e+02, 3.569930e+02, 3.894559e+02, 4.219189e+02, 4.543820e+02, 4.868449e+02, 5.193079e+02, 5.517709e+02, 5.842339e+02, 6.166970e+02, 6.491600e+02, 6.816229e+02, 7.140859e+02], dtype=float32)
-
x(x)float32-3324.4707 -3292.0078 ... 5343.1504
- standard_name :
- projection_x_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoX
array([-3324.4707, -3292.0078, -3259.5447, ..., 5278.2246, 5310.6875, 5343.1504], dtype=float32)
-
-
-
u-component_of_wind_isobaric(time1, y, x)float32...
- long_name :
- u-component of wind @ Isobaric surface
- units :
- m s^-1
- description :
- u-component of wind
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-33_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 33
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
array([[[ -5.377701, -5.393326, ..., -14.471451, -15.330826], [ -5.533951, -5.112076, ..., -15.065201, -15.346451], ..., [ -3.143326, -2.705826, ..., 7.794174, 7.091049], [ -2.549576, -2.721451, ..., 8.512924, 7.497299]]], dtype=float32)
-
LambertConformal_Projection()int320
- grid_mapping_name :
- lambert_conformal_conic
- latitude_of_projection_origin :
- 50.000003814697266
- longitude_of_central_meridian :
- -107.00000762939453
- standard_parallel :
- 50.000003814697266
- earth_radius :
- 6367470.0
- _CoordinateTransformType :
- Projection
- _CoordinateAxisTypes :
- GeoX GeoY
array(0, dtype=int32)
-
lat(y, x)float6417.87 17.96 18.06 ... 34.85 34.64
- units :
- degrees_north
- long_name :
- latitude coordinate
- standard_name :
- latitude
- _CoordinateAxisType :
- Lat
array([[17.871157, 17.96379 , 18.055705, ..., 11.171237, 11.04458 , 10.917531], [18.107957, 18.201085, 18.293491, ..., 11.374014, 11.246746, 11.119084], [18.345107, 18.43873 , 18.53163 , ..., 11.57699 , 11.449107, 11.320831], ..., [46.323111, 46.49107 , 46.657954, ..., 34.691863, 34.480463, 34.268685], [46.56076 , 46.72954 , 46.897241, ..., 34.879394, 34.667184, 34.454598], [46.79791 , 46.967512, 47.136034, ..., 35.066338, 34.853317, 34.639922]])
-
lon(y, x)float64-135.0 -134.8 ... -43.13 -42.91
- units :
- degrees_east
- long_name :
- longitude coordinate
- standard_name :
- longitude
- _CoordinateAxisType :
- Lon
array([[-135.006462, -134.757515, -134.50792 , ..., -65.276918, -65.070682, -64.865157], [-135.104494, -134.854843, -134.604538, ..., -65.14766 , -64.941078, -64.735212], [-135.203181, -134.952825, -134.701807, ..., -65.017683, -64.810756, -64.60455 ], ..., [-153.092434, -152.746829, -152.398966, ..., -43.878114, -43.650466, -43.424375], [-153.337548, -152.991143, -152.642454, ..., -43.620454, -43.393 , -43.167112], [-153.584937, -153.237739, -152.88823 , ..., -43.361216, -43.133969, -42.908296]])
-
Geopotential_height_isobaric(time1, y, x)float32...
- long_name :
- Geopotential height @ Isobaric surface
- units :
- gpm
- description :
- Geopotential height
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-7_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 7
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
array([[[153.24702 , 153.24702 , ..., 103.247025, 102.247025], [155.74702 , 155.24702 , ..., 105.747025, 105.247025], ..., [139.74702 , 141.74702 , ..., 201.74702 , 204.74702 ], [138.24702 , 140.24702 , ..., 200.24702 , 203.74702 ]]], dtype=float32)
-
v-component_of_wind_isobaric(time1, y, x)float32...
- long_name :
- v-component of wind @ Isobaric surface
- units :
- m s^-1
- description :
- v-component of wind
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-34_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 34
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
array([[[-5.755432, -5.739807, ..., -4.724182, -4.583557], [-5.989807, -6.005432, ..., -3.833557, -4.583557], ..., [ 4.635193, 4.541443, ..., 9.494568, 9.103943], [ 5.228943, 4.541443, ..., 9.822693, 9.275818]]], dtype=float32)
-
Temperature_isobaric(time1, y, x)float32...
- long_name :
- Temperature @ Isobaric surface
- units :
- K
- description :
- Temperature
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-11_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 11
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
array([[[294.09436, 294.1256 , ..., 298.6725 , 298.71936], [294.07874, 294.14124, ..., 298.6256 , 298.5475 ], ..., [276.86 , 276.84436, ..., 289.11 , 289.01624], [277.01624, 276.82874, ..., 289.0475 , 288.96936]]], dtype=float32)
-
-
- Originating_or_generating_Center :
- US National Weather Service, National Centres for Environmental Prediction (NCEP)
- Originating_or_generating_Subcenter :
- North American Regional Reanalysis Project
- GRIB_table_version :
- 0,131
- Generating_process_or_model :
- North American Regional Reanalysis (NARR)
- Conventions :
- CF-1.6
- history :
- Read using CDM IOSP GribCollection v3
- featureType :
- GRID
- History :
- Translated to CF-1.0 Conventions by Netcdf-Java CDM (CFGridWriter2) Original Dataset = DatasetScan#narr-a_221_19930313_0000_000.grb; Translation Date = 2018-01-04T23:57:16.043Z
- geospatial_lat_min :
- 10.753308882144761
- geospatial_lat_max :
- 46.8308828962289
- geospatial_lon_min :
- -153.88242040519995
- geospatial_lon_max :
- -42.666108129242815
ds_1000.Temperature_isobaric
<xarray.DataArray 'Temperature_isobaric' (time1: 1, y: 119, x: 268)> array([[[294.09436, 294.1256 , ..., 298.6725 , 298.71936], [294.07874, 294.14124, ..., 298.6256 , 298.5475 ], ..., [276.86 , 276.84436, ..., 289.11 , 289.01624], [277.01624, 276.82874, ..., 289.0475 , 288.96936]]], dtype=float32) Coordinates: * time1 (time1) datetime64[ns] 1993-03-13 isobaric1 float32 1000.0 * y (y) float32 -3116.548 -3084.0852 -3051.622 ... 681.6229 714.08594 * x (x) float32 -3324.4707 -3292.0078 ... 5310.6875 5343.1504 Attributes: long_name: Temperature @ Isobaric surface units: K description: Temperature grid_mapping: LambertConformal_Projection Grib_Variable_Id: VAR_7-15-131-11_L100 Grib1_Center: 7 Grib1_Subcenter: 15 Grib1_TableVersion: 131 Grib1_Parameter: 11 Grib1_Level_Type: 100 Grib1_Level_Desc: Isobaric surface
- time1: 1
- y: 119
- x: 268
- 294.09436 294.1256 294.11 294.11 ... 289.14124 289.0475 288.96936
array([[[294.09436, 294.1256 , ..., 298.6725 , 298.71936], [294.07874, 294.14124, ..., 298.6256 , 298.5475 ], ..., [276.86 , 276.84436, ..., 289.11 , 289.01624], [277.01624, 276.82874, ..., 289.0475 , 288.96936]]], dtype=float32)
-
-
time1(time1)datetime64[ns]1993-03-13
- standard_name :
- time
- long_name :
- GRIB forecast or observation time
- _CoordinateAxisType :
- Time
array(['1993-03-13T00:00:00.000000000'], dtype='datetime64[ns]')
-
isobaric1()float321000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array(1000., dtype=float32)
-
y(y)float32-3116.548 -3084.0852 ... 714.08594
- standard_name :
- projection_y_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoY
array([-3.116548e+03, -3.084085e+03, -3.051622e+03, -3.019159e+03, -2.986696e+03, -2.954233e+03, -2.921770e+03, -2.889307e+03, -2.856844e+03, -2.824381e+03, -2.791918e+03, -2.759455e+03, -2.726992e+03, -2.694529e+03, -2.662066e+03, -2.629603e+03, -2.597140e+03, -2.564677e+03, -2.532214e+03, -2.499751e+03, -2.467288e+03, -2.434825e+03, -2.402362e+03, -2.369899e+03, -2.337436e+03, -2.304973e+03, -2.272510e+03, -2.240047e+03, -2.207584e+03, -2.175121e+03, -2.142658e+03, -2.110195e+03, -2.077732e+03, -2.045269e+03, -2.012806e+03, -1.980343e+03, -1.947880e+03, -1.915417e+03, -1.882954e+03, -1.850491e+03, -1.818028e+03, -1.785565e+03, -1.753102e+03, -1.720639e+03, -1.688176e+03, -1.655713e+03, -1.623250e+03, -1.590787e+03, -1.558324e+03, -1.525861e+03, -1.493398e+03, -1.460935e+03, -1.428472e+03, -1.396009e+03, -1.363546e+03, -1.331083e+03, -1.298620e+03, -1.266157e+03, -1.233694e+03, -1.201231e+03, -1.168768e+03, -1.136305e+03, -1.103842e+03, -1.071379e+03, -1.038916e+03, -1.006453e+03, -9.739901e+02, -9.415271e+02, -9.090641e+02, -8.766011e+02, -8.441381e+02, -8.116751e+02, -7.792121e+02, -7.467491e+02, -7.142861e+02, -6.818231e+02, -6.493601e+02, -6.168971e+02, -5.844341e+02, -5.519711e+02, -5.195081e+02, -4.870451e+02, -4.545821e+02, -4.221191e+02, -3.896561e+02, -3.571931e+02, -3.247301e+02, -2.922671e+02, -2.598041e+02, -2.273411e+02, -1.948781e+02, -1.624151e+02, -1.299521e+02, -9.748907e+01, -6.502608e+01, -3.256308e+01, -1.000744e-01, 3.236293e+01, 6.482593e+01, 9.728893e+01, 1.297519e+02, 1.622149e+02, 1.946779e+02, 2.271409e+02, 2.596039e+02, 2.920669e+02, 3.245299e+02, 3.569930e+02, 3.894559e+02, 4.219189e+02, 4.543820e+02, 4.868449e+02, 5.193079e+02, 5.517709e+02, 5.842339e+02, 6.166970e+02, 6.491600e+02, 6.816229e+02, 7.140859e+02], dtype=float32)
-
x(x)float32-3324.4707 -3292.0078 ... 5343.1504
- standard_name :
- projection_x_coordinate
- units :
- km
- _CoordinateAxisType :
- GeoX
array([-3324.4707, -3292.0078, -3259.5447, ..., 5278.2246, 5310.6875, 5343.1504], dtype=float32)
-
-
- long_name :
- Temperature @ Isobaric surface
- units :
- K
- description :
- Temperature
- grid_mapping :
- LambertConformal_Projection
- Grib_Variable_Id :
- VAR_7-15-131-11_L100
- Grib1_Center :
- 7
- Grib1_Subcenter :
- 15
- Grib1_TableVersion :
- 131
- Grib1_Parameter :
- 11
- Grib1_Level_Type :
- 100
- Grib1_Level_Desc :
- Isobaric surface
Aggregation operations¶
Not only can you use the named dimensions for manual slicing and indexing of data, but you can also use it to control aggregation operations, like sum
:
u_winds = ds['u-component_of_wind_isobaric']
u_winds.std(dim=['x', 'y'])
<xarray.DataArray 'u-component_of_wind_isobaric' (time1: 1, isobaric1: 29)> array([[ 8.673963 , 10.212325 , 11.556413 , 12.254429 , 13.372146 , 15.472462 , 16.091969 , 15.846294 , 15.195834 , 13.936979 , 12.93888 , 12.060708 , 10.972139 , 9.722328 , 8.853286 , 8.257241 , 7.679721 , 7.4516497, 7.2352104, 7.039894 , 6.883371 , 6.7821493, 6.7088237, 6.6865997, 6.7247376, 6.745023 , 6.6859775, 6.5107226, 5.972262 ]], dtype=float32) Coordinates: * time1 (time1) datetime64[ns] 1993-03-13 * isobaric1 (isobaric1) float32 100.0 125.0 150.0 ... 950.0 975.0 1000.0
- time1: 1
- isobaric1: 29
- 8.673963 10.212325 11.556413 ... 6.6859775 6.5107226 5.972262
array([[ 8.673963 , 10.212325 , 11.556413 , 12.254429 , 13.372146 , 15.472462 , 16.091969 , 15.846294 , 15.195834 , 13.936979 , 12.93888 , 12.060708 , 10.972139 , 9.722328 , 8.853286 , 8.257241 , 7.679721 , 7.4516497, 7.2352104, 7.039894 , 6.883371 , 6.7821493, 6.7088237, 6.6865997, 6.7247376, 6.745023 , 6.6859775, 6.5107226, 5.972262 ]], dtype=float32)
-
-
time1(time1)datetime64[ns]1993-03-13
- standard_name :
- time
- long_name :
- GRIB forecast or observation time
- _CoordinateAxisType :
- Time
array(['1993-03-13T00:00:00.000000000'], dtype='datetime64[ns]')
-
isobaric1(isobaric1)float32100.0 125.0 150.0 ... 975.0 1000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
- x: -182km to 424km
- y: -1450km to -990km
# YOUR CODE GOES HERE
# %load solutions/mean_profile.py
# Cell content replaced by load magic replacement.
temps = ds.Temperature_isobaric
co_temps = temps.sel(x=slice(-182, 424), y=slice(-1450, -990))
prof = co_temps.mean(dim=['x', 'y'])
prof
<xarray.DataArray 'Temperature_isobaric' (time1: 1, isobaric1: 29)> array([[215.078 , 215.76935, 217.243 , 217.82663, 215.83487, 216.10933, 219.99902, 224.66118, 228.80576, 234.88701, 238.78503, 242.66309, 246.44807, 249.26636, 250.84995, 253.37354, 257.0429 , 259.08398, 260.97955, 262.98364, 264.82138, 266.5198 , 268.22467, 269.7471 , 271.18216, 272.66815, 274.13037, 275.54718, 276.97675]], dtype=float32) Coordinates: * time1 (time1) datetime64[ns] 1993-03-13 * isobaric1 (isobaric1) float32 100.0 125.0 150.0 ... 950.0 975.0 1000.0
- time1: 1
- isobaric1: 29
- 215.078 215.76935 217.243 217.82663 ... 274.13037 275.54718 276.97675
array([[215.078 , 215.76935, 217.243 , 217.82663, 215.83487, 216.10933, 219.99902, 224.66118, 228.80576, 234.88701, 238.78503, 242.66309, 246.44807, 249.26636, 250.84995, 253.37354, 257.0429 , 259.08398, 260.97955, 262.98364, 264.82138, 266.5198 , 268.22467, 269.7471 , 271.18216, 272.66815, 274.13037, 275.54718, 276.97675]], dtype=float32)
-
-
time1(time1)datetime64[ns]1993-03-13
- standard_name :
- time
- long_name :
- GRIB forecast or observation time
- _CoordinateAxisType :
- Time
array(['1993-03-13T00:00:00.000000000'], dtype='datetime64[ns]')
-
isobaric1(isobaric1)float32100.0 125.0 150.0 ... 975.0 1000.0
- units :
- hPa
- long_name :
- Isobaric surface
- positive :
- down
- Grib_level_type :
- 100
- _CoordinateAxisType :
- Pressure
- _CoordinateZisPositive :
- down
array([ 100., 125., 150., 175., 200., 225., 250., 275., 300., 350., 400., 450., 500., 550., 600., 650., 700., 725., 750., 775., 800., 825., 850., 875., 900., 925., 950., 975., 1000.], dtype=float32)
-
-
Resources¶
There is much more in the XArray library. To learn more, visit the XArray Documentation