Xarray : Saving the results#

This example illustrates how to export and load your results. In details, we are going to show how to save and reload a single DataArray such as a Dataset.

import numpy as np
import xarray as xr
import pandas as pd
from time import sleep

Simulate data#

lets start by creating two random spatio-temporal arrays

n_times = 30
n_roi = 7
times_vec = np.linspace(-1, 1, n_times)
roi_vec = np.array([f"roi_{k}" for k in range(n_roi)])

# xarray.DataArray conversion
arr_1 = xr.DataArray(np.random.rand(n_times, n_roi), dims=('times', 'roi'),
                     coords=(times_vec, roi_vec))
arr_2 = xr.DataArray(np.random.rand(n_times, n_roi), dims=('times', 'roi'),
                     coords=(times_vec, roi_vec))

# just add a few attributes to each array
arr_1.attrs['desc'] = "This is my first array"
arr_1.attrs['sf'] = 1024.
arr_2.attrs['desc'] = "This is my second array"
arr_2.attrs['sf'] = 512.

# note that you can also concatenate DataArray
arr_cat = xr.concat([arr_1, arr_2], 'roi')

Export and load a single DataArray#

now we’re going to save a single array and then reload it

# export a single array
arr_1.to_netcdf("first_array.nc")

# delete it
del arr_1
sleep(3)

# reload it
arr_1 = xr.load_dataarray("first_array.nc")
print(arr_1)
<xarray.DataArray (times: 30, roi: 7)>
array([[0.86857235, 0.34164905, 0.07372154, 0.89789325, 0.35990704,
        0.65792827, 0.55258436],
       [0.36755137, 0.28709102, 0.59027736, 0.82513448, 0.91369344,
        0.46232561, 0.80131533],
       [0.08382459, 0.03989172, 0.93011621, 0.3325575 , 0.61165937,
        0.22830203, 0.51993939],
       [0.93127553, 0.23276973, 0.98737781, 0.09400561, 0.63267185,
        0.89516765, 0.86630896],
       [0.63931787, 0.55889173, 0.07318019, 0.34548094, 0.96125968,
        0.41724228, 0.84738602],
       [0.95116317, 0.42952837, 0.37866289, 0.96935173, 0.74483427,
        0.99808698, 0.51873042],
       [0.69646825, 0.42380644, 0.99963565, 0.6276689 , 0.76244809,
        0.90018115, 0.53439585],
       [0.23354597, 0.10547792, 0.65069225, 0.32769162, 0.379606  ,
        0.57783361, 0.19340793],
       [0.54923492, 0.4537632 , 0.9489388 , 0.24208163, 0.33178174,
        0.02667819, 0.20008582],
       [0.42848129, 0.92694796, 0.81405107, 0.28176009, 0.68551371,
        0.0503056 , 0.92729583],
...
       [0.51478955, 0.75137336, 0.75859279, 0.29150561, 0.75212268,
        0.26095436, 0.7898412 ],
       [0.98046669, 0.6948562 , 0.32646186, 0.40960364, 0.67467995,
        0.51850242, 0.54622083],
       [0.61769449, 0.6003606 , 0.14811608, 0.88057523, 0.74589634,
        0.6340674 , 0.28570341],
       [0.66192625, 0.76261038, 0.54061787, 0.89168368, 0.86898578,
        0.02466154, 0.27301619],
       [0.35095706, 0.50846419, 0.12115092, 0.5902835 , 0.11717464,
        0.20221288, 0.50209237],
       [0.64928082, 0.44053491, 0.68582699, 0.21394309, 0.76422177,
        0.63199195, 0.74964898],
       [0.99248584, 0.07150736, 0.08374601, 0.53922115, 0.40193095,
        0.14991542, 0.99623634],
       [0.3306397 , 0.8246187 , 0.74346867, 0.88813752, 0.62566109,
        0.42568013, 0.29383443],
       [0.40620986, 0.22138854, 0.69523441, 0.46879049, 0.7818004 ,
        0.43932892, 0.88447955],
       [0.2832179 , 0.34052117, 0.5629731 , 0.74048449, 0.37796047,
        0.57859816, 0.3738478 ]])
Coordinates:
  * times    (times) float64 -1.0 -0.931 -0.8621 -0.7931 ... 0.8621 0.931 1.0
  * roi      (roi) object 'roi_0' 'roi_1' 'roi_2' ... 'roi_4' 'roi_5' 'roi_6'
Attributes:
    desc:     This is my first array
    sf:       1024.0

Export and load multiple DataArrays#

it’s also possible to export and reload multiple DataArrays at once. To do it, you can use a Dataset which is a container of DataArrays

# create a dataset
dat = xr.Dataset({'first': arr_1, 'second': arr_2})

# you can also slice the dataset and also add attributes to it
dat.attrs['desc'] = 'This is my dataset'
dat.attrs['sf'] = 256.

# export your dataset
dat.to_netcdf('full_dataset.nc')

# delete it
del dat
sleep(3)

# reload it
dat = xr.load_dataset("full_dataset.nc")
print(dat)

# finally, accessing array of a dataset is similar of using dictionary
arr_1 = dat['first']
arr_2 = dat['second']
<xarray.Dataset>
Dimensions:  (times: 30, roi: 7)
Coordinates:
  * times    (times) float64 -1.0 -0.931 -0.8621 -0.7931 ... 0.8621 0.931 1.0
  * roi      (roi) object 'roi_0' 'roi_1' 'roi_2' ... 'roi_4' 'roi_5' 'roi_6'
Data variables:
    first    (times, roi) float64 0.8686 0.3416 0.07372 ... 0.378 0.5786 0.3738
    second   (times, roi) float64 0.6484 0.7608 0.1135 ... 0.004891 0.4305
Attributes:
    desc:     This is my dataset
    sf:       256.0

Total running time of the script: (0 minutes 6.446 seconds)

Estimated memory usage: 18 MB

Gallery generated by Sphinx-Gallery