-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdims_untar.m
53 lines (42 loc) · 1.43 KB
/
dims_untar.m
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
%==========================================================================
%
%
% input :
%
% output :
%
% Siqi Li, SMAST
% 2022-10-03
%
% Updates:
%
%==========================================================================
function var1 = dims_untar(var2, dims1, mode)
% dims2 = size(var2, 1:4);
dims2(1) = size(var2, 1);
dims2(2) = size(var2, 2);
dims2(3) = size(var2, 3);
dims2(4) = size(var2, 4);
switch mode
% MODE 1: Keep the first and untar the last dimension back.
% e.g. (node, nsiglay*nt) ---> (node, nsiglay, nt)
case 1
var1 = reshape(var2, [dims2(1) dims1(2:end)]);
% MODE 2: Untar the first dimension back and untar the
% e.g. (nlon*nlat, nz, nt) ---> (nlon, nlat, nz, nt)
case 2
var1 = reshape(var2, [dims1(1:2) dims2(2:end)]);
% MODE 3: Untar the first dimension and the second dimension back, seperately
% e.g. (nlon*nlat, nz*nt) ---> (nlon, nlat, nz, nt)
case 3
var1 = dims_untar(var2, dims1, 2);
var1 = dims_untar(var1, dims1, 1);
% MODE 4: Keep the last dimension and tar the rest.
% e.g. (node*nsiglay, nt) ---> (node, nsiglay, nt)
% e.g. (nlon*nlat*nz, nt) ---> (nlon, nlat, nz, nt)
case 4
dims2 = size(var2);
var1 = reshape(var2, [dims1(1:end-1) dims2(end)]);
otherwise
error('UNKOWN mode.')
end