Skip to content

Commit 7e0a18a

Browse files
authored
Merge pull request #322 from wmvanvliet/xarray-fixes
Remove now unneeded notification and separate code and output blocks.
2 parents 04a68d4 + 4d6949f commit 7e0a18a

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

content/xarray.rst

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ Xarray is a powerful Python library that introduces labelled multidimensional ar
4545

4646
We will first download a dataset similar to the example above to illustrate the advantages of Xarray. We will cover how to transform your own data into an Xarray Dataset later in this lecture.
4747

48-
.. Note::
49-
50-
If you have set up your ``python-for-scicomp`` environment yesterday or earlier, you need to install the packages ``netcdf4`` and ``pythia_datasets`` manually. You can do this by running the following command in your (JupyterLab) terminal: ::
51-
52-
conda install netcdf4 pythia-datasets -c conda-forge
53-
5448
Let us open a python shell and download a public dataset: ::
5549
5650
>>> from pythia_datasets import DATASETS
@@ -61,6 +55,9 @@ We can now import xarray and open the dataset. Le'ts take a look at what it cont
6155
>>> import xarray as xr
6256
>>> ds = xr.open_dataset(filepath)
6357
>>> ds
58+
59+
Output: ::
60+
6461
<xarray.Dataset> Size: 15MB
6562
Dimensions: (time1: 1, isobaric1: 29, y: 119, x: 268)
6663
Coordinates:
@@ -111,6 +108,9 @@ We can select a single ``DataArray`` from the dataset using a dictionary-like sy
111108

112109
>>> temperature_data = ds['Temperature_isobaric']
113110
>>> temperature_data
111+
112+
Output: ::
113+
114114
<xarray.DataArray 'Temperature_isobaric' (time1: 1, isobaric1: 29, y: 119,
115115
x: 268)> Size: 4MB
116116
[924868 values with dtype=float32]
@@ -138,6 +138,9 @@ Xarray uses Numpy(-like) arrays under the hood, we can always access the underly
138138

139139
>>> temperature_numpy = ds['Temperature_isobaric'].values
140140
>>> temperature_numpy
141+
142+
Output: ::
143+
141144
array([[[[201.88957, 202.2177 , 202.49895, ..., 195.10832, 195.23332,
142145
195.37395],
143146
[201.68645, 202.0302 , 202.3427 , ..., 195.24895, 195.38957,
@@ -156,6 +159,9 @@ Xarray uses Numpy(-like) arrays under the hood, we can always access the underly
156159
Xarray allows you to select data using the ``.sel()`` method, which uses the labels of the dimensions to extract data: ::
157160

158161
>>> ds['Temperature_isobaric'].sel(x='-3292.0078')
162+
163+
Output: ::
164+
159165
<xarray.DataArray 'Temperature_isobaric' (time1: 1, isobaric1: 29, y: 119)> Size: 14kB
160166
array([[[202.2177 , 202.0302 , ..., 219.67082, 219.74895],
161167
[202.58566, 202.58566, ..., 219.16379, 219.28879],
@@ -184,6 +190,9 @@ Xarray allows you to select data using the ``.sel()`` method, which uses the lab
184190
We can still access the same data by index using the ``.isel()`` method: ::
185191

186192
>>> ds['Temperature_isobaric'].isel(x=1)
193+
194+
Output: ::
195+
187196
<xarray.DataArray 'Temperature_isobaric' (time1: 1, isobaric1: 29, y: 119)> Size: 14kB
188197
array([[[202.2177 , 202.0302 , ..., 219.67082, 219.74895],
189198
[202.58566, 202.58566, ..., 219.16379, 219.28879],
@@ -213,6 +222,9 @@ A ``DataArray`` provides a lot of the functionality we expect from Numpy arrays,
213222

214223
>>> # Calculate the mean over the 'isobaric1' dimension
215224
>>> ds['Temperature_isobaric'].mean(dim='isobaric1')
225+
226+
Output: ::
227+
216228
<xarray.DataArray 'Temperature_isobaric' (time1: 1, y: 119, x: 268)> Size: 128kB
217229
array([[[259.88446, 259.90222, 259.91678, ..., 262.61667, 262.6285 ,
218230
262.65167],
@@ -237,6 +249,9 @@ Let's take a look at a concrete example and compare it to NumPy. We will calcula
237249

238250
>>> # Xarray
239251
>>> ds['Temperature_isobaric'].sel(x='-3259.5447').max(dim='isobaric1')
252+
253+
Output: ::
254+
240255
array([[294.11 , 294.14124, 294.1256 , 294.0475 , 293.90686, 293.6256 ,
241256
...,
242257
276.46936, 276.59436, 276.6881 , 276.78186, 276.82874]],
@@ -246,6 +261,9 @@ In comparison, if we were to use plain Numpy, this would be: ::
246261

247262
>>> # NumPy
248263
>>> np.max(temperature_numpy[:, :, :, 2 ], axis = 1)
264+
265+
Output: ::
266+
249267
array([[294.11 , 294.14124, 294.1256 , 294.0475 , 293.90686, 293.6256 ,
250268
...,
251269
276.46936, 276.59436, 276.6881 , 276.78186, 276.82874]],

0 commit comments

Comments
 (0)