forked from brainets/frites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_xr_export.py
80 lines (63 loc) · 2.21 KB
/
plot_xr_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
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
###############################################################################
# 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
# reload it
arr_1 = xr.load_dataarray("first_array.nc")
print(arr_1)
###############################################################################
# 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
# 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']