1+ #!/usr/bin/env python
2+
3+ import mxarray as mx
4+ import numpy as np
5+ import xarray as xr
6+ import sys
7+
8+ def test_roundtrip (path ):
9+
10+ # create xarray
11+ arr = xr .DataArray (
12+ np .arange (3 * 4 * 5 * 6 , dtype = np .float32 ).reshape (3 ,4 ,5 ,6 ),
13+ dims = ("t" ,"x" ,"y" ,"z" ),
14+ coords = {
15+ "t" : [0 ,1 ,2 ],
16+ "x" : [0.0 , 1.0 , 2.0 , 3.0 ],
17+ "y" : [0.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
18+ "z" : [0.0 , 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]
19+ }
20+ )
21+ # write to RSF
22+ out = path
23+ mx .xarray_to_rsf (arr , str (out ))
24+
25+ # read back
26+ da = mx .rsf_to_xarray (str (out ))
27+ diff = da - arr
28+
29+ assert da .shape == (3 , 4 , 5 , 6 )
30+ assert da .values .dtype == arr .values .dtype == np .float32
31+ assert da .dims == arr .dims
32+ assert da .coords ["t" ].values .tolist () == arr .coords ["t" ].values .tolist ()
33+ assert da .coords ["x" ].values .tolist () == arr .coords ["x" ].values .tolist ()
34+ assert da .coords ["y" ].values .tolist () == arr .coords ["y" ].values .tolist ()
35+ assert da .coords ["z" ].values .tolist () == arr .coords ["z" ].values .tolist ()
36+ assert da .values .tolist () == arr .values .tolist ()
37+ assert diff .values .tolist () == [[[[0.0 for _ in range (6 )] for _ in range (5 )] for _ in range (4 )] for _ in range (3 )]
38+
39+ # read as xarray Dataset
40+ arr = mx .rsf_to_xarrayds (path )
41+
42+ # write to netcdf (can be loaded in paraview)
43+ arr .to_netcdf ("test.nc" )
44+ arr2 = xr .open_dataarray ("test.nc" )
45+
46+ assert arr .sizes == arr2 .sizes
47+ assert arr .coords ["t" ].values .tolist () == arr2 .coords ["t" ].values .tolist ()
48+ assert arr .coords ["x" ].values .tolist () == arr2 .coords ["x" ].values .tolist ()
49+ assert arr .coords ["y" ].values .tolist () == arr2 .coords ["y" ].values .tolist ()
50+ assert arr .coords ["z" ].values .tolist () == arr2 .coords ["z" ].values .tolist ()
51+ assert arr .data .values .tolist () == arr2 .values .tolist ()
52+
53+ import m8r , os
54+ tmp = m8r .Input (file )
55+ binFile = tmp .string ("in" )
56+ os .remove (binFile )
57+ os .remove (file )
58+ os .remove ("test.nc" )
59+
60+ def test_cunks (path ):
61+ arr = xr .DataArray (
62+ np .arange (100 * 100 * 100 * 5 , dtype = np .float32 ).reshape (100 ,100 ,100 ,5 ),
63+ dims = ("t" ,"x" ,"y" ,"z" ),
64+ coords = {
65+ "t" : np .arange (100 ),
66+ "x" : np .arange (100 ),
67+ "y" : np .arange (100 ),
68+ "z" : np .arange (5 )
69+ }
70+ )
71+
72+ mx .xarray_to_rsf (arr , path )
73+ del arr
74+
75+ import time
76+ start_time = time .time ()
77+ da = mx .rsf_to_xarray (path , chunks = (2 , 3 , 5 , 1 ))
78+ print (f"mean: { da .mean ().compute ().values } " )
79+ end_time = time .time ()
80+ print (f"read with chuks { (2 , 3 , 5 , 1 )} took { end_time - start_time } seconds" )
81+ del da
82+ start_time = time .time ()
83+ da = mx .rsf_to_xarray (path , chunks = (1 , 1 ,1 , 1 ))
84+ print (f"mean: { da .mean ().compute ().values } " )
85+ end_time = time .time ()
86+ print (f"read with chuks { (1 , 1 , 1 , 1 )} took { end_time - start_time } seconds" )
87+ del da
88+ start_time = time .time ()
89+ da = mx .rsf_to_xarray (path , chunks = "auto" )
90+ print (f"mean: { da .mean ().compute ().values } " )
91+ end_time = time .time ()
92+ print (f"read with chuks auto took { end_time - start_time } seconds" )
93+ del da
94+
95+ import os
96+ os .remove (path )
97+
98+
99+ if __name__ == "__main__" :
100+ file = "tmp.rsf"
101+ test_roundtrip (path = file )
102+ # test_cunks(path="./tmp_large.rsf")
103+ sys .stdout .write ("All tests passed.\n " )
104+ ## test chunks
105+ ####################################################################################
106+ ############# output for data with shape (100, 100, 100, 5) ########################
107+ ####################################################################################
108+ # mean: 2499999.5
109+ # read with chuks (2, 3, 5, 1) took 21.33984923362732 seconds
110+ # mean: 2499999.5
111+ # read with chuks (1, 1, 1, 1) took 559.3831532001495 seconds
112+ # mean: 2499999.5
113+ # read with chuks auto took 0.01817178726196289 seconds
114+ ####################################################################################
0 commit comments