Reading and writing files
This tutorial includes an overview of the different ways available to load the binary arrays from the disc after running a numerical simulation with XCompact3d .
Besides that, some options are presented to save the results from our analysis, together with some tips and tricks.
Preparation
Here we prepare the dataset for this notebook, so it can be reproduced on local machines or on the cloud, you are invited to test and interact with many of the concepts.
It also provides nice support for courses and tutorials, let us know if you produce any of them.
The very first step is to import the toolbox and other packages:
Then we can download an example from the online database , the flow around a cylinder in this case.
We set cache=True
and a local destination where it can be saved in our computer cache_dir="./example/"
, so there is no need to download it every time the kernel is restarted.
let’s take a look at the dataset:
xarray.Dataset {
dimensions:
i = 2 ;
x = 257 ;
y = 128 ;
t = 201 ;
variables:
float32 u(i, x, y, t) ;
float32 pp(x, y, t) ;
float32 epsi(x, y) ;
float64 x(x) ;
float64 y(y) ;
float64 t(t) ;
<U1 i(i) ;
// global attributes:
:xcompact3d_version = v3.0-397-gff531df ;
:xcompact3d_toolbox_version = 1.0.1 ;
:url = https://github.com/fschuch/xcompact3d_toolbox_data ;
:dataset_license = MIT License ;
}
We got a xarray.Dataset with the variables u
(velocity vector), pp
(pressure) and epsi
(describes the geometry), their coordinates (x
, y
, t
and i
) and some attributes like the xcompact3d_version
used to run this simulation, the url
where you can find the dataset, and others.
In the next block, we configure the toolbox and some attributes at the dataset, so we can write all the binary fields to the disc.
Do not worry about the details right now, this is just the preparation step, we are going to discuss them later.
After that, the files are organized as follow:
tutorial
│ computing_and_plotting.ipynb
│ io.ipynb
│ input.i3d
│ parameters.ipynb
│ xy-planes.xdmf
│
└─── data
│ │ epsilon.bin
│ │ pp-000.bin
│ │ pp-001.bin
│ │ ...
│ │ pp-199.bin
│ │ pp-200.bin
│ │ ux-000.bin
│ │ ux-001.bin
│ │ ...
│ │ ux-199.bin
│ │ ux-200.bin
│ │ uy-000.bin
│ │ uy-001.bin
│ │ ...
│ │ uy-199.bin
│ │ uy-200.bin
│ │ uz-000.bin
│ │ uz-001.bin
│ │ ...
│ │ uz-199.bin
│ │ uz-200.bin
│
└─── example
│ │ cylinder.nc
It is very similar to what we get after successfully running a simulation, so now we can move on to the tutorial.
Why xarray?
The data structures are provided by xarray , that introduces labels in the form of dimensions, coordinates and attributes on top of raw NumPy-like arrays, which allows for a more intuitive, more concise, and less error-prone developer experience.
It integrates tightly with dask for parallel computing.
The goal here is to speed up the development of customized post-processing applications with the concise interface provided by xarray . Ultimately, we can compute solutions with fewer lines of code and better readability, so we expend less time testing and debugging and more time exploring our datasets and getting insights.
Additionally, xcompact3d-toolbox includes extra functionalities for DataArray and Dataset .
Before going forward, please, take a look at Overview: Why xarray? and Quick overview to understand the motivation to use xarray ’s data structures instead of just numpy-like arrays.
Xarray objects on demand
To start our post-processing, let’s load the parameters file:
Notice there is an entire tutorial dedicated to it .
To save space on the disc, our dataset was converted from double precision to single, so we have to configure the toolbox to:
The methods in the toolbox support different filename properties , like the classic ux000
or the new ux-0000.bin
, besides some combinations between them. For our case, we set the parameters as:
Now we specify the parameters for our dataset, like where it is found (data_path
), if it needs to drop some coordinate (drop_coords
, again, to save space, we are working with a span-wise averaged dataset, so we drop z
to work with xy
planes), we inform the parameter that controls the number of timesteps snapshot_counting
and their step snapshot_step
.
Consult the dataset documentation to see different ways to customize your experience, and choose the ones that best suits your post-processing application.
In this example, they are defined as:
Now we are good to go.
We can check the length of the dataset we are dealing with:
Meaning that our binary files range from 0 (i.g., ux-000.bin
) to 200 (i.g., ux-200.bin
), exactly as expected.
It is possible to load any given array:
Notice that load_array requires the entire path to the file, and we use add_time=False
because this array does not evolve in time like the others, i.e., it is not numerated for several snapshots.
We can see it on the screen:
<xarray.DataArray (x: 257, y: 128)> Size: 132kB
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32) Coordinates: (2)
Indexes: (2)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) Attributes: (0)
Let’s do it again, this time for ux
and using add_time=True
:
See that t
is now a coordinate, and for this snapshot it was computed automatically as dimensionless time 75.0
:
<xarray.DataArray 'ux' (x: 257, y: 128, t: 1)> Size: 132kB
array([[[1. ],
[1. ],
[1. ],
...,
[1. ],
[1. ],
[1. ]],
[[1.0000466 ],
[0.99996716],
[1.0000466 ],
...,
[0.9999681 ],
[1.0000459 ],
[0.9999675 ]],
[[1.0000602 ],
[1.0000144 ],
[1.0000602 ],
...,
...
...,
[1.0140737 ],
[1.0142432 ],
[1.0144366 ]],
[[1.0146521 ],
[1.0148891 ],
[1.0151588 ],
...,
[1.0141058 ],
[1.0142633 ],
[1.0144445 ]],
[[1.0146475 ],
[1.0148702 ],
[1.0151254 ],
...,
[1.014144 ],
[1.0142874 ],
[1.014454 ]]], dtype=float32)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 75.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.014 1.014 1.014 1.014 1.014 1.014
array([[[1. ],
[1. ],
[1. ],
...,
[1. ],
[1. ],
[1. ]],
[[1.0000466 ],
[0.99996716],
[1.0000466 ],
...,
[0.9999681 ],
[1.0000459 ],
[0.9999675 ]],
[[1.0000602 ],
[1.0000144 ],
[1.0000602 ],
...,
...
...,
[1.0140737 ],
[1.0142432 ],
[1.0144366 ]],
[[1.0146521 ],
[1.0148891 ],
[1.0151588 ],
...,
[1.0141058 ],
[1.0142633 ],
[1.0144445 ]],
[[1.0146475 ],
[1.0148702 ],
[1.0151254 ],
...,
[1.014144 ],
[1.0142874 ],
[1.014454 ]]], dtype=float32) Coordinates: (3)
Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([75.0], dtype='float32', name='t')) Attributes: (0)
That is not all. If you have enough memory, you can load the entire time series for a given variable with load_time_series , or simply by:
Let’s see it (note 201 files are loaded and wrapped with the appropriate coordinates):
<xarray.DataArray 'ux' (x: 257, y: 128, t: 201)> Size: 26MB
array([[[0.9999885 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999782 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999907 , 1. , 1. , ..., 1. ,
1. , 1. ],
...,
[1.0000122 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.99999744, 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999945 , 1. , 1. , ..., 1. ,
1. , 1. ]],
[[0.99999535, 1.0000458 , 1.0000486 , ..., 1.0000464 ,
1.0000468 , 1.0000476 ],
[1.0000107 , 0.99997103, 0.99996907, ..., 0.9999675 ,
0.99996674, 0.9999665 ],
[1.0000069 , 1.0000455 , 1.0000486 , ..., 1.0000459 ,
1.0000468 , 1.0000478 ],
...
[0.9999908 , 1.0001078 , 1.000192 , ..., 1.0155317 ,
1.0153327 , 1.0146557 ],
[0.9999987 , 1.0001054 , 1.0001911 , ..., 1.0152053 ,
1.0150691 , 1.0146141 ],
[0.9999874 , 1.0000997 , 1.0001901 , ..., 1.0149139 ,
1.0148364 , 1.0145978 ]],
[[1.0000043 , 1.0000877 , 1.0001792 , ..., 1.0146514 ,
1.0146394 , 1.0146087 ],
[1.0000119 , 1.0000889 , 1.0001761 , ..., 1.014433 ,
1.0144511 , 1.0146273 ],
[1.0000004 , 1.0000932 , 1.000174 , ..., 1.0142416 ,
1.0142918 , 1.0146769 ],
...,
[1.0000123 , 1.0000978 , 1.0001832 , ..., 1.0154953 ,
1.0153852 , 1.0147215 ],
[0.99998474, 1.000096 , 1.0001816 , ..., 1.0151795 ,
1.0151051 , 1.0146575 ],
[0.99999154, 1.0000918 , 1.0001806 , ..., 1.0148991 ,
1.0148568 , 1.0146192 ]]], dtype=float32)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 804B 0.0 0.75 1.5 2.25 3.0 ... 147.8 148.5 149.2 150.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.014 1.014 1.015 1.015 1.015 1.015
array([[[0.9999885 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999782 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999907 , 1. , 1. , ..., 1. ,
1. , 1. ],
...,
[1.0000122 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.99999744, 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999945 , 1. , 1. , ..., 1. ,
1. , 1. ]],
[[0.99999535, 1.0000458 , 1.0000486 , ..., 1.0000464 ,
1.0000468 , 1.0000476 ],
[1.0000107 , 0.99997103, 0.99996907, ..., 0.9999675 ,
0.99996674, 0.9999665 ],
[1.0000069 , 1.0000455 , 1.0000486 , ..., 1.0000459 ,
1.0000468 , 1.0000478 ],
...
[0.9999908 , 1.0001078 , 1.000192 , ..., 1.0155317 ,
1.0153327 , 1.0146557 ],
[0.9999987 , 1.0001054 , 1.0001911 , ..., 1.0152053 ,
1.0150691 , 1.0146141 ],
[0.9999874 , 1.0000997 , 1.0001901 , ..., 1.0149139 ,
1.0148364 , 1.0145978 ]],
[[1.0000043 , 1.0000877 , 1.0001792 , ..., 1.0146514 ,
1.0146394 , 1.0146087 ],
[1.0000119 , 1.0000889 , 1.0001761 , ..., 1.014433 ,
1.0144511 , 1.0146273 ],
[1.0000004 , 1.0000932 , 1.000174 , ..., 1.0142416 ,
1.0142918 , 1.0146769 ],
...,
[1.0000123 , 1.0000978 , 1.0001832 , ..., 1.0154953 ,
1.0153852 , 1.0147215 ],
[0.99998474, 1.000096 , 1.0001816 , ..., 1.0151795 ,
1.0151051 , 1.0146575 ],
[0.99999154, 1.0000918 , 1.0001806 , ..., 1.0148991 ,
1.0148568 , 1.0146192 ]]], dtype=float32) Coordinates: (3)
x
(x)
float32
0.0 0.07812 0.1562 ... 19.92 20.0
array([ 0. , 0.078125, 0.15625 , ..., 19.84375 , 19.921875, 20. ],
dtype=float32) y
(y)
float32
0.0 0.09375 0.1875 ... 11.81 11.91
array([ 0. , 0.09375, 0.1875 , 0.28125, 0.375 , 0.46875, 0.5625 ,
0.65625, 0.75 , 0.84375, 0.9375 , 1.03125, 1.125 , 1.21875,
1.3125 , 1.40625, 1.5 , 1.59375, 1.6875 , 1.78125, 1.875 ,
1.96875, 2.0625 , 2.15625, 2.25 , 2.34375, 2.4375 , 2.53125,
2.625 , 2.71875, 2.8125 , 2.90625, 3. , 3.09375, 3.1875 ,
3.28125, 3.375 , 3.46875, 3.5625 , 3.65625, 3.75 , 3.84375,
3.9375 , 4.03125, 4.125 , 4.21875, 4.3125 , 4.40625, 4.5 ,
4.59375, 4.6875 , 4.78125, 4.875 , 4.96875, 5.0625 , 5.15625,
5.25 , 5.34375, 5.4375 , 5.53125, 5.625 , 5.71875, 5.8125 ,
5.90625, 6. , 6.09375, 6.1875 , 6.28125, 6.375 , 6.46875,
6.5625 , 6.65625, 6.75 , 6.84375, 6.9375 , 7.03125, 7.125 ,
7.21875, 7.3125 , 7.40625, 7.5 , 7.59375, 7.6875 , 7.78125,
7.875 , 7.96875, 8.0625 , 8.15625, 8.25 , 8.34375, 8.4375 ,
8.53125, 8.625 , 8.71875, 8.8125 , 8.90625, 9. , 9.09375,
9.1875 , 9.28125, 9.375 , 9.46875, 9.5625 , 9.65625, 9.75 ,
9.84375, 9.9375 , 10.03125, 10.125 , 10.21875, 10.3125 , 10.40625,
10.5 , 10.59375, 10.6875 , 10.78125, 10.875 , 10.96875, 11.0625 ,
11.15625, 11.25 , 11.34375, 11.4375 , 11.53125, 11.625 , 11.71875,
11.8125 , 11.90625], dtype=float32) t
(t)
float32
0.0 0.75 1.5 ... 148.5 149.2 150.0
array([ 0. , 0.75, 1.5 , ..., 148.5 , 149.25, 150. ], dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([ 0.0, 0.75, 1.5, 2.25, 3.0, 3.75, 4.5, 5.25, 6.0,
6.75,
...
143.25, 144.0, 144.75, 145.5, 146.25, 147.0, 147.75, 148.5, 149.25,
150.0],
dtype='float32', name='t', length=201)) Attributes: (0)
You can store each array in a different variable, like:
Or organize many arrays in a dataset:
<xarray.Dataset> Size: 79MB
Dimensions: (x: 257, y: 128, t: 201)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 804B 0.0 0.75 1.5 2.25 3.0 ... 147.8 148.5 149.2 150.0
Data variables:
ux (x, y, t) float32 26MB 1.0 1.0 1.0 1.0 ... 1.015 1.015 1.015 1.015
uy (x, y, t) float32 26MB 9.98e-06 8.496e-08 ... -0.0005357 0.003209
pp (x, y, t) float32 26MB 0.0 0.03264 0.03613 ... 0.03922 0.03859 Dimensions:
Coordinates: (3)
x
(x)
float32
0.0 0.07812 0.1562 ... 19.92 20.0
array([ 0. , 0.078125, 0.15625 , ..., 19.84375 , 19.921875, 20. ],
dtype=float32) y
(y)
float32
0.0 0.09375 0.1875 ... 11.81 11.91
array([ 0. , 0.09375, 0.1875 , 0.28125, 0.375 , 0.46875, 0.5625 ,
0.65625, 0.75 , 0.84375, 0.9375 , 1.03125, 1.125 , 1.21875,
1.3125 , 1.40625, 1.5 , 1.59375, 1.6875 , 1.78125, 1.875 ,
1.96875, 2.0625 , 2.15625, 2.25 , 2.34375, 2.4375 , 2.53125,
2.625 , 2.71875, 2.8125 , 2.90625, 3. , 3.09375, 3.1875 ,
3.28125, 3.375 , 3.46875, 3.5625 , 3.65625, 3.75 , 3.84375,
3.9375 , 4.03125, 4.125 , 4.21875, 4.3125 , 4.40625, 4.5 ,
4.59375, 4.6875 , 4.78125, 4.875 , 4.96875, 5.0625 , 5.15625,
5.25 , 5.34375, 5.4375 , 5.53125, 5.625 , 5.71875, 5.8125 ,
5.90625, 6. , 6.09375, 6.1875 , 6.28125, 6.375 , 6.46875,
6.5625 , 6.65625, 6.75 , 6.84375, 6.9375 , 7.03125, 7.125 ,
7.21875, 7.3125 , 7.40625, 7.5 , 7.59375, 7.6875 , 7.78125,
7.875 , 7.96875, 8.0625 , 8.15625, 8.25 , 8.34375, 8.4375 ,
8.53125, 8.625 , 8.71875, 8.8125 , 8.90625, 9. , 9.09375,
9.1875 , 9.28125, 9.375 , 9.46875, 9.5625 , 9.65625, 9.75 ,
9.84375, 9.9375 , 10.03125, 10.125 , 10.21875, 10.3125 , 10.40625,
10.5 , 10.59375, 10.6875 , 10.78125, 10.875 , 10.96875, 11.0625 ,
11.15625, 11.25 , 11.34375, 11.4375 , 11.53125, 11.625 , 11.71875,
11.8125 , 11.90625], dtype=float32) t
(t)
float32
0.0 0.75 1.5 ... 148.5 149.2 150.0
array([ 0. , 0.75, 1.5 , ..., 148.5 , 149.25, 150. ], dtype=float32) Data variables: (3)
ux
(x, y, t)
float32
1.0 1.0 1.0 ... 1.015 1.015 1.015
array([[[0.9999885 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999782 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999907 , 1. , 1. , ..., 1. ,
1. , 1. ],
...,
[1.0000122 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.99999744, 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999945 , 1. , 1. , ..., 1. ,
1. , 1. ]],
[[0.99999535, 1.0000458 , 1.0000486 , ..., 1.0000464 ,
1.0000468 , 1.0000476 ],
[1.0000107 , 0.99997103, 0.99996907, ..., 0.9999675 ,
0.99996674, 0.9999665 ],
[1.0000069 , 1.0000455 , 1.0000486 , ..., 1.0000459 ,
1.0000468 , 1.0000478 ],
...
[0.9999908 , 1.0001078 , 1.000192 , ..., 1.0155317 ,
1.0153327 , 1.0146557 ],
[0.9999987 , 1.0001054 , 1.0001911 , ..., 1.0152053 ,
1.0150691 , 1.0146141 ],
[0.9999874 , 1.0000997 , 1.0001901 , ..., 1.0149139 ,
1.0148364 , 1.0145978 ]],
[[1.0000043 , 1.0000877 , 1.0001792 , ..., 1.0146514 ,
1.0146394 , 1.0146087 ],
[1.0000119 , 1.0000889 , 1.0001761 , ..., 1.014433 ,
1.0144511 , 1.0146273 ],
[1.0000004 , 1.0000932 , 1.000174 , ..., 1.0142416 ,
1.0142918 , 1.0146769 ],
...,
[1.0000123 , 1.0000978 , 1.0001832 , ..., 1.0154953 ,
1.0153852 , 1.0147215 ],
[0.99998474, 1.000096 , 1.0001816 , ..., 1.0151795 ,
1.0151051 , 1.0146575 ],
[0.99999154, 1.0000918 , 1.0001806 , ..., 1.0148991 ,
1.0148568 , 1.0146192 ]]], dtype=float32) uy
(x, y, t)
float32
9.98e-06 8.496e-08 ... 0.003209
array([[[ 9.97988354e-06, 8.49550474e-08, 7.13089747e-08, ...,
-8.24902656e-07, -2.60075126e-06, -2.86581985e-06],
[ 1.95531647e-05, -7.69747430e-08, -6.73220626e-08, ...,
3.28200031e-07, 2.38231701e-06, 3.15269222e-06],
[ 3.23466520e-06, 7.58641789e-08, 6.87333710e-08, ...,
-8.22868401e-07, -2.60304864e-06, -2.86488989e-06],
...,
[-6.78599736e-06, -6.34202166e-08, -6.34171471e-08, ...,
3.27393138e-07, 2.38772441e-06, 3.14991462e-06],
[ 5.60609988e-06, 9.24364798e-08, 7.38993293e-08, ...,
-8.25677262e-07, -2.59799208e-06, -2.86752447e-06],
[ 4.75289698e-06, -6.79251002e-08, -6.53620873e-08, ...,
3.26972867e-07, 2.38457028e-06, 3.15169700e-06]],
[[ 8.17127875e-06, -5.76831951e-07, 2.31006970e-06, ...,
1.04692674e-06, -2.17211462e-04, -2.06557655e-04],
[-9.58256805e-06, -2.44471339e-05, -2.38329212e-05, ...,
-2.73195601e-05, -2.43618590e-04, -2.29676007e-04],
[-1.56297228e-05, -4.50514381e-05, -4.61988166e-05, ...,
-4.24469617e-05, -2.60806963e-04, -2.51242163e-04],
...
-7.39102345e-03, 5.73747639e-05, 3.82003514e-03],
[-3.16069963e-06, -2.01298026e-04, -1.89392798e-04, ...,
-7.35661760e-03, 6.12066970e-06, 3.76115902e-03],
[ 1.06863872e-05, -1.87718950e-04, -1.75856665e-04, ...,
-7.33621093e-03, -3.47090245e-05, 3.71675403e-03]],
[[ 1.14502300e-05, -1.77631853e-04, -1.64712968e-04, ...,
-7.52553809e-03, -5.55378327e-04, 3.18954163e-03],
[-4.91587525e-06, -1.66795871e-04, -1.52920198e-04, ...,
-7.52645032e-03, -5.47196891e-04, 3.20842443e-03],
[ 2.38375487e-05, -1.55161368e-04, -1.41728029e-04, ...,
-7.54353357e-03, -5.31221100e-04, 3.24137555e-03],
...,
[-5.17192575e-06, -2.13182488e-04, -2.02694311e-04, ...,
-7.60111213e-03, -4.52007138e-04, 3.31370509e-03],
[-2.28461449e-05, -1.99920411e-04, -1.90276100e-04, ...,
-7.56283524e-03, -5.08141296e-04, 3.24189453e-03],
[ 1.96180008e-05, -1.87553553e-04, -1.77575974e-04, ...,
-7.53604714e-03, -5.35702275e-04, 3.20866751e-03]]],
dtype=float32) pp
(x, y, t)
float32
0.0 0.03264 ... 0.03922 0.03859
array([[[ 0. , 0.03264126, 0.03612559, ..., 0.05173054,
0.05321342, 0.0555568 ],
[ 0. , 0.03249451, 0.03596627, ..., 0.0515916 ,
0.05332436, 0.05563498],
[ 0. , 0.03269685, 0.03618003, ..., 0.05181858,
0.05380777, 0.05609087],
...,
[ 0. , 0.03260394, 0.03610066, ..., 0.05163186,
0.05236094, 0.05479917],
[ 0. , 0.03269532, 0.03619223, ..., 0.05175101,
0.05272993, 0.05513581],
[ 0. , 0.03249378, 0.03597238, ..., 0.05155691,
0.05278588, 0.05515926]],
[[ 0. , 0.03256193, 0.03604122, ..., 0.05164916,
0.05313087, 0.05547334],
[ 0. , 0.03255073, 0.03602634, ..., 0.05165312,
0.05338719, 0.05569855],
[ 0. , 0.03261778, 0.03609589, ..., 0.05173779,
0.05372536, 0.05600698],
...
[ 0. , -0.00597118, -0.00516765, ..., 0.04166358,
0.04048561, 0.04038416],
[ 0. , -0.00597305, -0.00516866, ..., 0.04122475,
0.03985312, 0.03948712],
[ 0. , -0.00597509, -0.00516978, ..., 0.04078778,
0.03922178, 0.03859093]],
[[ 0. , -0.00597691, -0.00517167, ..., 0.04035036,
0.03859136, 0.03769449],
[ 0. , -0.00597803, -0.00517183, ..., 0.03991735,
0.03795951, 0.03679422],
[ 0. , -0.00598033, -0.00517296, ..., 0.03948206,
0.03732474, 0.03588924],
...,
[ 0. , -0.00597219, -0.00516751, ..., 0.04165559,
0.04048872, 0.04039202],
[ 0. , -0.00597349, -0.00516917, ..., 0.04121848,
0.03985493, 0.03949196],
[ 0. , -0.00597574, -0.00516981, ..., 0.04078456,
0.03922356, 0.03859415]]], dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([ 0.0, 0.75, 1.5, 2.25, 3.0, 3.75, 4.5, 5.25, 6.0,
6.75,
...
143.25, 144.0, 144.75, 145.5, 146.25, 147.0, 147.75, 148.5, 149.25,
150.0],
dtype='float32', name='t', length=201)) Attributes: (0)
We can also write an one-liner solution for the previous code:
ds = xr . Dataset ({ var : prm . dataset [ var ] for var in "ux uy pp" . split ()})
It is possible to load all the variables from a given snapshot with load_snapshot , or simply:
And we got a xarray.Dataset with all the variables and their coordinates. You can access each of them with the dot notation (i.g., snapshot.pp
, snapshot.ux
, snapshot.uy
) or the dict-like notation (i.g., snapshot["pp"]
, snapshot["ux"]
, snapshot["uy"]
). See the dataset:
<xarray.Dataset> Size: 396kB
Dimensions: (x: 257, y: 128, t: 1)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 75.0
Data variables:
pp (x, y, t) float32 132kB 0.05232 0.05219 0.05243 ... 0.03986 0.03989
ux (x, y, t) float32 132kB 1.0 1.0 1.0 1.0 ... 1.014 1.014 1.014 1.014
uy (x, y, t) float32 132kB 3.407e-07 1.503e-07 ... 0.007724 0.007703 Dimensions:
Coordinates: (3)
Data variables: (3)
pp
(x, y, t)
float32
0.05232 0.05219 ... 0.03986 0.03989
array([[[0.05232061],
[0.05219164],
[0.05243129],
...,
[0.05219198],
[0.05231902],
[0.0521366 ]],
[[0.05223907],
[0.05225366],
[0.05234953],
...,
[0.05225244],
[0.05223813],
[0.05219809]],
[[0.05220748],
[0.05222414],
[0.05231803],
...,
...
...,
[0.03980655],
[0.03984426],
[0.03988079]],
[[0.03991373],
[0.0399437 ],
[0.0399735 ],
...,
[0.03981845],
[0.03985148],
[0.03988307]],
[[0.03991202],
[0.03993938],
[0.03996518],
...,
[0.03982718],
[0.0398563 ],
[0.03988532]]], dtype=float32) ux
(x, y, t)
float32
1.0 1.0 1.0 ... 1.014 1.014 1.014
array([[[1. ],
[1. ],
[1. ],
...,
[1. ],
[1. ],
[1. ]],
[[1.0000466 ],
[0.99996716],
[1.0000466 ],
...,
[0.9999681 ],
[1.0000459 ],
[0.9999675 ]],
[[1.0000602 ],
[1.0000144 ],
[1.0000602 ],
...,
...
...,
[1.0140737 ],
[1.0142432 ],
[1.0144366 ]],
[[1.0146521 ],
[1.0148891 ],
[1.0151588 ],
...,
[1.0141058 ],
[1.0142633 ],
[1.0144445 ]],
[[1.0146475 ],
[1.0148702 ],
[1.0151254 ],
...,
[1.014144 ],
[1.0142874 ],
[1.014454 ]]], dtype=float32) uy
(x, y, t)
float32
3.407e-07 1.503e-07 ... 0.007703
array([[[ 3.4073531e-07],
[ 1.5034894e-07],
[ 3.4228231e-07],
...,
[ 1.4578703e-07],
[ 3.3796812e-07],
[ 1.4887451e-07]],
[[-3.8211783e-05],
[-5.7585352e-05],
[-8.1790080e-05],
...,
[ 3.8404520e-05],
[ 5.6476906e-06],
[-9.7466746e-06]],
[[-9.5541123e-05],
[-1.4544252e-04],
[-1.8961391e-04],
...,
...
...,
[ 7.6859673e-03],
[ 7.6573351e-03],
[ 7.6414165e-03]],
[[ 7.7379229e-03],
[ 7.7515850e-03],
[ 7.7808392e-03],
...,
[ 7.7937511e-03],
[ 7.7590533e-03],
[ 7.7407509e-03]],
[[ 7.7001532e-03],
[ 7.7144550e-03],
[ 7.7471440e-03],
...,
[ 7.7630985e-03],
[ 7.7244956e-03],
[ 7.7031888e-03]]], dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([75.0], dtype='float32', name='t')) Attributes: (0)
Do you need the snapshots in a range? No problem. Let’s do a slice to load the last 100, and just to exemplify, compute a time average :
<xarray.Dataset> Size: 396kB
Dimensions: (x: 257, y: 128)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
Data variables:
pp (x, y) float32 132kB 0.05351 0.05335 0.05356 ... 0.03886 0.03886
ux (x, y) float32 132kB 1.0 1.0 1.0 1.0 ... 1.015 1.015 1.015 1.015
uy (x, y) float32 132kB -6.206e-09 2.081e-09 ... -6.504e-05 -6.531e-05 Dimensions:
Coordinates: (2)
Data variables: (3)
pp
(x, y)
float32
0.05351 0.05335 ... 0.03886 0.03886
array([[0.05350577, 0.05334765, 0.05356117, ..., 0.05345961, 0.0535612 ,
0.05334766],
[0.0534233 , 0.05341006, 0.05347893, ..., 0.05352153, 0.05347894,
0.05341007],
[0.05339126, 0.05337996, 0.05344687, ..., 0.05349133, 0.05344689,
0.05337997],
...,
[0.03886158, 0.03885861, 0.03885458, ..., 0.03886537, 0.03886495,
0.03886379],
[0.03886124, 0.03885819, 0.03885429, ..., 0.03886476, 0.03886453,
0.0388633 ],
[0.03886105, 0.03885868, 0.03885414, ..., 0.03886512, 0.03886428,
0.03886374]], dtype=float32) ux
(x, y)
float32
1.0 1.0 1.0 ... 1.015 1.015 1.015
array([[1. , 1. , 1. , ..., 1. , 1. ,
1. ],
[1.000047 , 0.9999672, 1.0000467, ..., 0.9999675, 1.0000467,
0.9999672],
[1.0000609, 1.0000151, 1.0000609, ..., 1.0000153, 1.0000609,
1.0000149],
...,
[1.014633 , 1.0146457, 1.0146878, ..., 1.0147717, 1.014696 ,
1.0146499],
[1.0146337, 1.0146464, 1.0146886, ..., 1.014772 , 1.0146965,
1.0146502],
[1.0146341, 1.0146468, 1.0146891, ..., 1.0147722, 1.0146966,
1.0146506]], dtype=float32) uy
(x, y)
float32
-6.206e-09 2.081e-09 ... -6.531e-05
array([[-6.2061507e-09, 2.0807329e-09, -6.2151546e-09, ...,
2.1238338e-09, -6.1867742e-09, 2.0955053e-09],
[ 1.2998876e-07, -2.4157436e-05, -4.4205062e-05, ...,
7.2888673e-05, 4.4463806e-05, 2.4339783e-05],
[ 4.2155676e-07, -4.4589146e-05, -9.5065327e-05, ...,
1.3574678e-04, 9.5904950e-05, 4.5521323e-05],
...,
[-6.4251049e-05, -6.5562090e-05, -6.6985704e-05, ...,
-6.0804643e-05, -6.1885214e-05, -6.3014835e-05],
[-6.4824388e-05, -6.5339118e-05, -6.5953936e-05, ...,
-6.3980537e-05, -6.4151391e-05, -6.4441243e-05],
[-6.5726759e-05, -6.6256522e-05, -6.6938665e-05, ...,
-6.4873107e-05, -6.5041131e-05, -6.5311178e-05]], dtype=float32) Indexes: (2)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) Attributes: (0)
You can even use the slice notation to load all the snapshots at once:
<xarray.Dataset> Size: 79MB
Dimensions: (x: 257, y: 128, t: 201)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 804B 0.0 0.75 1.5 2.25 3.0 ... 147.8 148.5 149.2 150.0
Data variables:
pp (x, y, t) float32 26MB 0.0 0.03264 0.03613 ... 0.03922 0.03859
ux (x, y, t) float32 26MB 1.0 1.0 1.0 1.0 ... 1.015 1.015 1.015 1.015
uy (x, y, t) float32 26MB 9.98e-06 8.496e-08 ... -0.0005357 0.003209 Dimensions:
Coordinates: (3)
x
(x)
float32
0.0 0.07812 0.1562 ... 19.92 20.0
array([ 0. , 0.078125, 0.15625 , ..., 19.84375 , 19.921875, 20. ],
dtype=float32) y
(y)
float32
0.0 0.09375 0.1875 ... 11.81 11.91
array([ 0. , 0.09375, 0.1875 , 0.28125, 0.375 , 0.46875, 0.5625 ,
0.65625, 0.75 , 0.84375, 0.9375 , 1.03125, 1.125 , 1.21875,
1.3125 , 1.40625, 1.5 , 1.59375, 1.6875 , 1.78125, 1.875 ,
1.96875, 2.0625 , 2.15625, 2.25 , 2.34375, 2.4375 , 2.53125,
2.625 , 2.71875, 2.8125 , 2.90625, 3. , 3.09375, 3.1875 ,
3.28125, 3.375 , 3.46875, 3.5625 , 3.65625, 3.75 , 3.84375,
3.9375 , 4.03125, 4.125 , 4.21875, 4.3125 , 4.40625, 4.5 ,
4.59375, 4.6875 , 4.78125, 4.875 , 4.96875, 5.0625 , 5.15625,
5.25 , 5.34375, 5.4375 , 5.53125, 5.625 , 5.71875, 5.8125 ,
5.90625, 6. , 6.09375, 6.1875 , 6.28125, 6.375 , 6.46875,
6.5625 , 6.65625, 6.75 , 6.84375, 6.9375 , 7.03125, 7.125 ,
7.21875, 7.3125 , 7.40625, 7.5 , 7.59375, 7.6875 , 7.78125,
7.875 , 7.96875, 8.0625 , 8.15625, 8.25 , 8.34375, 8.4375 ,
8.53125, 8.625 , 8.71875, 8.8125 , 8.90625, 9. , 9.09375,
9.1875 , 9.28125, 9.375 , 9.46875, 9.5625 , 9.65625, 9.75 ,
9.84375, 9.9375 , 10.03125, 10.125 , 10.21875, 10.3125 , 10.40625,
10.5 , 10.59375, 10.6875 , 10.78125, 10.875 , 10.96875, 11.0625 ,
11.15625, 11.25 , 11.34375, 11.4375 , 11.53125, 11.625 , 11.71875,
11.8125 , 11.90625], dtype=float32) t
(t)
float32
0.0 0.75 1.5 ... 148.5 149.2 150.0
array([ 0. , 0.75, 1.5 , ..., 148.5 , 149.25, 150. ], dtype=float32) Data variables: (3)
pp
(x, y, t)
float32
0.0 0.03264 ... 0.03922 0.03859
array([[[ 0. , 0.03264126, 0.03612559, ..., 0.05173054,
0.05321342, 0.0555568 ],
[ 0. , 0.03249451, 0.03596627, ..., 0.0515916 ,
0.05332436, 0.05563498],
[ 0. , 0.03269685, 0.03618003, ..., 0.05181858,
0.05380777, 0.05609087],
...,
[ 0. , 0.03260394, 0.03610066, ..., 0.05163186,
0.05236094, 0.05479917],
[ 0. , 0.03269532, 0.03619223, ..., 0.05175101,
0.05272993, 0.05513581],
[ 0. , 0.03249378, 0.03597238, ..., 0.05155691,
0.05278588, 0.05515926]],
[[ 0. , 0.03256193, 0.03604122, ..., 0.05164916,
0.05313087, 0.05547334],
[ 0. , 0.03255073, 0.03602634, ..., 0.05165312,
0.05338719, 0.05569855],
[ 0. , 0.03261778, 0.03609589, ..., 0.05173779,
0.05372536, 0.05600698],
...
[ 0. , -0.00597118, -0.00516765, ..., 0.04166358,
0.04048561, 0.04038416],
[ 0. , -0.00597305, -0.00516866, ..., 0.04122475,
0.03985312, 0.03948712],
[ 0. , -0.00597509, -0.00516978, ..., 0.04078778,
0.03922178, 0.03859093]],
[[ 0. , -0.00597691, -0.00517167, ..., 0.04035036,
0.03859136, 0.03769449],
[ 0. , -0.00597803, -0.00517183, ..., 0.03991735,
0.03795951, 0.03679422],
[ 0. , -0.00598033, -0.00517296, ..., 0.03948206,
0.03732474, 0.03588924],
...,
[ 0. , -0.00597219, -0.00516751, ..., 0.04165559,
0.04048872, 0.04039202],
[ 0. , -0.00597349, -0.00516917, ..., 0.04121848,
0.03985493, 0.03949196],
[ 0. , -0.00597574, -0.00516981, ..., 0.04078456,
0.03922356, 0.03859415]]], dtype=float32) ux
(x, y, t)
float32
1.0 1.0 1.0 ... 1.015 1.015 1.015
array([[[0.9999885 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999782 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999907 , 1. , 1. , ..., 1. ,
1. , 1. ],
...,
[1.0000122 , 1. , 1. , ..., 1. ,
1. , 1. ],
[0.99999744, 1. , 1. , ..., 1. ,
1. , 1. ],
[0.9999945 , 1. , 1. , ..., 1. ,
1. , 1. ]],
[[0.99999535, 1.0000458 , 1.0000486 , ..., 1.0000464 ,
1.0000468 , 1.0000476 ],
[1.0000107 , 0.99997103, 0.99996907, ..., 0.9999675 ,
0.99996674, 0.9999665 ],
[1.0000069 , 1.0000455 , 1.0000486 , ..., 1.0000459 ,
1.0000468 , 1.0000478 ],
...
[0.9999908 , 1.0001078 , 1.000192 , ..., 1.0155317 ,
1.0153327 , 1.0146557 ],
[0.9999987 , 1.0001054 , 1.0001911 , ..., 1.0152053 ,
1.0150691 , 1.0146141 ],
[0.9999874 , 1.0000997 , 1.0001901 , ..., 1.0149139 ,
1.0148364 , 1.0145978 ]],
[[1.0000043 , 1.0000877 , 1.0001792 , ..., 1.0146514 ,
1.0146394 , 1.0146087 ],
[1.0000119 , 1.0000889 , 1.0001761 , ..., 1.014433 ,
1.0144511 , 1.0146273 ],
[1.0000004 , 1.0000932 , 1.000174 , ..., 1.0142416 ,
1.0142918 , 1.0146769 ],
...,
[1.0000123 , 1.0000978 , 1.0001832 , ..., 1.0154953 ,
1.0153852 , 1.0147215 ],
[0.99998474, 1.000096 , 1.0001816 , ..., 1.0151795 ,
1.0151051 , 1.0146575 ],
[0.99999154, 1.0000918 , 1.0001806 , ..., 1.0148991 ,
1.0148568 , 1.0146192 ]]], dtype=float32) uy
(x, y, t)
float32
9.98e-06 8.496e-08 ... 0.003209
array([[[ 9.97988354e-06, 8.49550474e-08, 7.13089747e-08, ...,
-8.24902656e-07, -2.60075126e-06, -2.86581985e-06],
[ 1.95531647e-05, -7.69747430e-08, -6.73220626e-08, ...,
3.28200031e-07, 2.38231701e-06, 3.15269222e-06],
[ 3.23466520e-06, 7.58641789e-08, 6.87333710e-08, ...,
-8.22868401e-07, -2.60304864e-06, -2.86488989e-06],
...,
[-6.78599736e-06, -6.34202166e-08, -6.34171471e-08, ...,
3.27393138e-07, 2.38772441e-06, 3.14991462e-06],
[ 5.60609988e-06, 9.24364798e-08, 7.38993293e-08, ...,
-8.25677262e-07, -2.59799208e-06, -2.86752447e-06],
[ 4.75289698e-06, -6.79251002e-08, -6.53620873e-08, ...,
3.26972867e-07, 2.38457028e-06, 3.15169700e-06]],
[[ 8.17127875e-06, -5.76831951e-07, 2.31006970e-06, ...,
1.04692674e-06, -2.17211462e-04, -2.06557655e-04],
[-9.58256805e-06, -2.44471339e-05, -2.38329212e-05, ...,
-2.73195601e-05, -2.43618590e-04, -2.29676007e-04],
[-1.56297228e-05, -4.50514381e-05, -4.61988166e-05, ...,
-4.24469617e-05, -2.60806963e-04, -2.51242163e-04],
...
-7.39102345e-03, 5.73747639e-05, 3.82003514e-03],
[-3.16069963e-06, -2.01298026e-04, -1.89392798e-04, ...,
-7.35661760e-03, 6.12066970e-06, 3.76115902e-03],
[ 1.06863872e-05, -1.87718950e-04, -1.75856665e-04, ...,
-7.33621093e-03, -3.47090245e-05, 3.71675403e-03]],
[[ 1.14502300e-05, -1.77631853e-04, -1.64712968e-04, ...,
-7.52553809e-03, -5.55378327e-04, 3.18954163e-03],
[-4.91587525e-06, -1.66795871e-04, -1.52920198e-04, ...,
-7.52645032e-03, -5.47196891e-04, 3.20842443e-03],
[ 2.38375487e-05, -1.55161368e-04, -1.41728029e-04, ...,
-7.54353357e-03, -5.31221100e-04, 3.24137555e-03],
...,
[-5.17192575e-06, -2.13182488e-04, -2.02694311e-04, ...,
-7.60111213e-03, -4.52007138e-04, 3.31370509e-03],
[-2.28461449e-05, -1.99920411e-04, -1.90276100e-04, ...,
-7.56283524e-03, -5.08141296e-04, 3.24189453e-03],
[ 1.96180008e-05, -1.87553553e-04, -1.77575974e-04, ...,
-7.53604714e-03, -5.35702275e-04, 3.20866751e-03]]],
dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([ 0.0, 0.75, 1.5, 2.25, 3.0, 3.75, 4.5, 5.25, 6.0,
6.75,
...
143.25, 144.0, 144.75, 145.5, 146.25, 147.0, 147.75, 148.5, 149.25,
150.0],
dtype='float32', name='t', length=201)) Attributes: (0)
Of course, some simulations may not fit in the memory like in this tutorial. For these cases we can iterate over all snapshots, loading them one by one:
Note that reversed(prm.dataset)
also works.
Or for better control, we can iterate over a selected range of snapshots loading them one by one. The arguments are the same of a classic range in Python:
<xarray.DataArray (y: 128, t: 1, x: 257)> Size: 132kB
array([[[-0.00268999, 0.00181677, 0.0001196 , ..., 0.00324146,
0.00302246, -0.01646465]],
[[-0.0034743 , -0.00406752, -0.00344934, ..., 0.0029434 ,
0.00254392, -0.01612649]],
[[-0.00303078, -0.00340789, -0.0030852 , ..., 0.0026302 ,
0.0024012 , -0.01718407]],
...,
[[-0.00178402, -0.00190772, -0.00161242, ..., 0.00415556,
0.00375753, -0.01497194]],
[[-0.00232817, -0.00317189, -0.00255065, ..., 0.00385816,
0.00362989, -0.01590095]],
[[-0.00263563, 0.00207518, 0.00038911, ..., 0.00355052,
0.00316822, -0.01547501]]], dtype=float32)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 149.2 -0.00269 0.001817 0.0001196 -0.0013 ... 0.003551 0.003168 -0.01548
array([[[-0.00268999, 0.00181677, 0.0001196 , ..., 0.00324146,
0.00302246, -0.01646465]],
[[-0.0034743 , -0.00406752, -0.00344934, ..., 0.0029434 ,
0.00254392, -0.01612649]],
[[-0.00303078, -0.00340789, -0.0030852 , ..., 0.0026302 ,
0.0024012 , -0.01718407]],
...,
[[-0.00178402, -0.00190772, -0.00161242, ..., 0.00415556,
0.00375753, -0.01497194]],
[[-0.00232817, -0.00317189, -0.00255065, ..., 0.00385816,
0.00362989, -0.01590095]],
[[-0.00263563, 0.00207518, 0.00038911, ..., 0.00355052,
0.00316822, -0.01547501]]], dtype=float32) Coordinates: (3)
Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([149.25], dtype='float32', name='t')) Attributes: (0)
Writing the results to binary files
In the last example we computed the vorticity but did nothing with it. This time, let’s write it to the disc using write :
The example above works for a xarray.DataArray . We can do it for a xarray.Dataset as well, but with one key difference. Only the arrays with an attribute called file_name
will be written. It is done to avoid overwriting the base fields (ux
, uy
, uz
, …) by accident.
Let’s rewrite the previous example to store vort
in the dataset ds
. We set an attribute file_name
to w3
, so the arrays will be written as w3-000.bin
, w3-001.bin
, w3-002.bin
, etc.
We are also suppressing warnings, because the application will tell us it can not save pp
, ux
and uy
, since they do not have a file_name
. But in fact, we do not want to rewrite them anyway.
See the code:
The method prm.dataset.write() writes the files as raw binaries in the same way that XCompact3d would do. It means you can read them at the flow solver and also process them on any other tool that you are already familiar with, including the toolbox.
For instance, we get w3
if we load snapshot 0 again:
<xarray.Dataset> Size: 528kB
Dimensions: (x: 257, y: 128, t: 1)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 0.0
Data variables:
pp (x, y, t) float32 132kB 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
ux (x, y, t) float32 132kB 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0
uy (x, y, t) float32 132kB 9.98e-06 1.955e-05 ... -2.285e-05 1.962e-05
w3 (x, y, t) float32 132kB 0.00038 -0.000863 ... -0.001097 4.864e-05 Dimensions:
Coordinates: (3)
Data variables: (4)
pp
(x, y, t)
float32
0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
array([[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
...
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]]], dtype=float32) ux
(x, y, t)
float32
1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
array([[[0.9999885 ],
[0.9999782 ],
[0.9999907 ],
...,
[1.0000122 ],
[0.99999744],
[0.9999945 ]],
[[0.99999535],
[1.0000107 ],
[1.0000069 ],
...,
[0.9999595 ],
[0.9999916 ],
[1.0000165 ]],
[[0.9999961 ],
[0.9999833 ],
[0.9999893 ],
...,
...
...,
[0.9999767 ],
[1.000011 ],
[0.9999879 ]],
[[1.0000291 ],
[1.0000066 ],
[1.0000228 ],
...,
[0.9999908 ],
[0.9999987 ],
[0.9999874 ]],
[[1.0000043 ],
[1.0000119 ],
[1.0000004 ],
...,
[1.0000123 ],
[0.99998474],
[0.99999154]]], dtype=float32) uy
(x, y, t)
float32
9.98e-06 1.955e-05 ... 1.962e-05
array([[[ 9.97988354e-06],
[ 1.95531647e-05],
[ 3.23466520e-06],
...,
[-6.78599736e-06],
[ 5.60609988e-06],
[ 4.75289698e-06]],
[[ 8.17127875e-06],
[-9.58256805e-06],
[-1.56297228e-05],
...,
[ 2.22768977e-05],
[-1.19880242e-05],
[ 1.55943108e-05]],
[[-7.73779175e-06],
[ 2.75870798e-06],
[ 6.36937921e-06],
...,
...
...,
[ 5.06950937e-06],
[-2.75268358e-05],
[ 1.18241824e-05]],
[[-3.40437009e-06],
[ 7.18877664e-06],
[ 2.21101054e-05],
...,
[ 8.57373834e-07],
[-3.16069963e-06],
[ 1.06863872e-05]],
[[ 1.14502300e-05],
[-4.91587525e-06],
[ 2.38375487e-05],
...,
[-5.17192575e-06],
[-2.28461449e-05],
[ 1.96180008e-05]]], dtype=float32) w3
(x, y, t)
float32
0.00038 -0.000863 ... 4.864e-05
array([[[ 3.7999949e-04],
[-8.6302852e-04],
[-1.2169171e-03],
...,
[ 5.3181400e-04],
[-3.4472678e-04],
[-6.9962436e-04]],
[[-4.3674174e-04],
[-6.3654225e-05],
[ 3.4481002e-04],
...,
[ 1.6624911e-04],
[-5.9153017e-04],
[ 7.3439372e-04]],
[[ 1.5946515e-04],
[ 2.3157787e-04],
[ 1.2911536e-04],
...,
...
...,
[-1.5627242e-04],
[-2.6290008e-04],
[ 1.3722491e-03]],
[[ 9.7848650e-04],
[-1.4690481e-04],
[ 2.7931578e-04],
...,
[-2.5280163e-04],
[ 3.5106039e-04],
[ 1.5018106e-04]],
[[-1.7982686e-04],
[-2.3540645e-04],
[-4.6544732e-04],
...,
[ 3.3861716e-04],
[-1.0970247e-03],
[ 4.8644899e-05]]], dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([0.0], dtype='float32', name='t')) Attributes: (0)
Update the xdmf file
After computing and writing new results to the disc, you can open them on any external tools, like Paraview or Visit. You can update the xdmf file to include the recently computed w3
. See the code:
Other formats
Xarray objects can be exported to many other formats, depending on your needs.
For instance, xarray.DataArray and xarray.Dataset can be written as netCDF . In this way, they will keep all dimensions, coordinates, and attributes. This format is easier to handle and share because the files are self-sufficient. It is the format used to download the dataset used in this tutorial, and it is a good alternative to use when sharing the results of your research.
Just to give you an estimation about the disk usage, the size of the dataset cylinder.nc
that we downloaded for this tutorial is 75.8 MB. The size of the folder ./data/
after producing the binary files in the same way that XCompact3d would do is 75.7 MB.
To exemplify the use of netCDF, let’s take one snapshot:
<xarray.Dataset> Size: 528kB
Dimensions: (x: 257, y: 128, t: 1)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 0.0
Data variables:
pp (x, y, t) float32 132kB 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0
ux (x, y, t) float32 132kB 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0
uy (x, y, t) float32 132kB 9.98e-06 1.955e-05 ... -2.285e-05 1.962e-05
w3 (x, y, t) float32 132kB 0.00038 -0.000863 ... -0.001097 4.864e-05 Dimensions:
Coordinates: (3)
Data variables: (4)
pp
(x, y, t)
float32
0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
array([[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
...
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.],
...,
[0.],
[0.],
[0.]]], dtype=float32) ux
(x, y, t)
float32
1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0
array([[[0.9999885 ],
[0.9999782 ],
[0.9999907 ],
...,
[1.0000122 ],
[0.99999744],
[0.9999945 ]],
[[0.99999535],
[1.0000107 ],
[1.0000069 ],
...,
[0.9999595 ],
[0.9999916 ],
[1.0000165 ]],
[[0.9999961 ],
[0.9999833 ],
[0.9999893 ],
...,
...
...,
[0.9999767 ],
[1.000011 ],
[0.9999879 ]],
[[1.0000291 ],
[1.0000066 ],
[1.0000228 ],
...,
[0.9999908 ],
[0.9999987 ],
[0.9999874 ]],
[[1.0000043 ],
[1.0000119 ],
[1.0000004 ],
...,
[1.0000123 ],
[0.99998474],
[0.99999154]]], dtype=float32) uy
(x, y, t)
float32
9.98e-06 1.955e-05 ... 1.962e-05
array([[[ 9.97988354e-06],
[ 1.95531647e-05],
[ 3.23466520e-06],
...,
[-6.78599736e-06],
[ 5.60609988e-06],
[ 4.75289698e-06]],
[[ 8.17127875e-06],
[-9.58256805e-06],
[-1.56297228e-05],
...,
[ 2.22768977e-05],
[-1.19880242e-05],
[ 1.55943108e-05]],
[[-7.73779175e-06],
[ 2.75870798e-06],
[ 6.36937921e-06],
...,
...
...,
[ 5.06950937e-06],
[-2.75268358e-05],
[ 1.18241824e-05]],
[[-3.40437009e-06],
[ 7.18877664e-06],
[ 2.21101054e-05],
...,
[ 8.57373834e-07],
[-3.16069963e-06],
[ 1.06863872e-05]],
[[ 1.14502300e-05],
[-4.91587525e-06],
[ 2.38375487e-05],
...,
[-5.17192575e-06],
[-2.28461449e-05],
[ 1.96180008e-05]]], dtype=float32) w3
(x, y, t)
float32
0.00038 -0.000863 ... 4.864e-05
array([[[ 3.7999949e-04],
[-8.6302852e-04],
[-1.2169171e-03],
...,
[ 5.3181400e-04],
[-3.4472678e-04],
[-6.9962436e-04]],
[[-4.3674174e-04],
[-6.3654225e-05],
[ 3.4481002e-04],
...,
[ 1.6624911e-04],
[-5.9153017e-04],
[ 7.3439372e-04]],
[[ 1.5946515e-04],
[ 2.3157787e-04],
[ 1.2911536e-04],
...,
...
...,
[-1.5627242e-04],
[-2.6290008e-04],
[ 1.3722491e-03]],
[[ 9.7848650e-04],
[-1.4690481e-04],
[ 2.7931578e-04],
...,
[-2.5280163e-04],
[ 3.5106039e-04],
[ 1.5018106e-04]],
[[-1.7982686e-04],
[-2.3540645e-04],
[-4.6544732e-04],
...,
[ 3.3861716e-04],
[-1.0970247e-03],
[ 4.8644899e-05]]], dtype=float32) Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([0.0], dtype='float32', name='t')) Attributes: (0)
Now, let’s include additional information for the ones that are going to use our data. You can set attributes for each array, coordinate, and also global attributes for the dataset. They are stored in a dictionary.
See the example:
Exporting it as a netCDF file:
Importing the netCDF file:
See the result, it keeps all dimensions, coordinates, and attributes:
<xarray.Dataset> Size: 528kB
Dimensions: (x: 257, y: 128, t: 1)
Coordinates:
* x (x) float32 1kB 0.0 0.07812 0.1562 0.2344 ... 19.84 19.92 20.0
* y (y) float32 512B 0.0 0.09375 0.1875 0.2812 ... 11.72 11.81 11.91
* t (t) float32 4B 0.0
Data variables:
pp (x, y, t) float32 132kB ...
ux (x, y, t) float32 132kB ...
uy (x, y, t) float32 132kB ...
w3 (x, y, t) float32 132kB ...
Attributes:
title: An example from the tutorials
url: https://docs.fschuch.com/xcompact3d_toolbox/tutorial/io.html
authors: List of names
doi: maybe a fancy doi from zenodo Dimensions:
Coordinates: (3)
Data variables: (4)
Indexes: (3)
PandasIndex
PandasIndex(Index([ 0.0, 0.078125, 0.15625, 0.234375, 0.3125, 0.390625,
0.46875, 0.546875, 0.625, 0.703125,
...
19.296875, 19.375, 19.453125, 19.53125, 19.609375, 19.6875,
19.765625, 19.84375, 19.921875, 20.0],
dtype='float32', name='x', length=257)) PandasIndex
PandasIndex(Index([ 0.0, 0.09375, 0.1875, 0.28125, 0.375, 0.46875, 0.5625,
0.65625, 0.75, 0.84375,
...
11.0625, 11.15625, 11.25, 11.34375, 11.4375, 11.53125, 11.625,
11.71875, 11.8125, 11.90625],
dtype='float32', name='y', length=128)) PandasIndex
PandasIndex(Index([0.0], dtype='float32', name='t')) Attributes: (4)
title : An example from the tutorials url : https://docs.fschuch.com/xcompact3d_toolbox/tutorial/io.html authors : List of names doi : maybe a fancy doi from zenodo
We can compare them and see that their data, dimensions and coordinates are exactly the same:
Xarray is built on top of Numpy, so you can access a numpy.ndarray
object with the property values
(i.g., epsilon.values
). It is compatible with numpy.save
and many other methods from the Numpy/SciPy ecosystem (many times, you do not even need to explicitly use .values
). See the example:
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
You can use it for backwards compatibility with your previous post-processing tools. It is just not so effective, because we lost track of metadata like the coordinates and attributes.
If you manage to reduce the dataset’s dimensions with some integration , mean , or selecting subsets of data, you can convert it to a pandas.Dataframe and then export it to CSV, Excel, and many other options.
For instance, let’s select a vertical profile for all variables where x = 20
and convert it to a dataframe :
x
pp
ux
uy
w3
y
t
0.00000
0.0
20.0
0.0
1.000004
0.000011
-0.000180
0.09375
0.0
20.0
0.0
1.000012
-0.000005
-0.000235
0.18750
0.0
20.0
0.0
1.000000
0.000024
-0.000465
0.28125
0.0
20.0
0.0
1.000002
-0.000019
-0.000969
0.37500
0.0
20.0
0.0
0.999997
-0.000018
0.002280
...
...
...
...
...
...
...
11.53125
0.0
20.0
0.0
1.000013
0.000023
0.000101
11.62500
0.0
20.0
0.0
1.000009
-0.000010
-0.002189
11.71875
0.0
20.0
0.0
1.000012
-0.000005
0.000339
11.81250
0.0
20.0
0.0
0.999985
-0.000023
-0.001097
11.90625
0.0
20.0
0.0
0.999992
0.000020
0.000049
128 rows × 5 columns
Now, you can refer to pandas documentation for more details.