-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc_def_header.m
121 lines (101 loc) · 2.44 KB
/
nc_def_header.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
%==========================================================================
% nc_def_header --- nc_toolbox
% Define header of a new NetCDF file
%
% input :
% nc --- nc struct
% |---name
% |---dims {dim_name, dim_length}
% |---atts {att_name, att_value}
% |---vars {var_name, var_type, var_dims, {att1_name, att1_value}, ...}
%
%
% output :
% \
%
% Siqi Li, SMAST
% 2022-04-06
%
% Updates:
%
%==========================================================================
function nc0 = nc_def_header(nc)
nc0.name = nc.name;
for i = 1 : length(nc.dims)
nc0.dims(i).name = nc.dims{i}{1};
nc0.dims(i).length = nc.dims{i}{2};
end
if isfield(nc, 'atts')
for i = 1 : length(nc.atts)
nc0.atts(i).name = nc.atts{i}{1};
nc0.atts(i).value = nc.atts{i}{2};
end
end
for i = 1 : length(nc.vars)
nc0.vars(i).name = nc.vars{i}{1};
nc0.vars(i).type = nc.vars{i}{2};
nc0.vars(i).dims = nc.vars{i}{3};
j = 3;
while j<length(nc.vars{i})
j = j+1;
nc0.vars(i).atts(j-3).name = nc.vars{i}{j}{1};
nc0.vars(i).atts(j-3).value = nc.vars{i}{j}{2};
end
end
nc_def_header0(nc0);
end
%==========================================================================
% nc_def_header0 --- nc_toolbox
% Define header of a new NetCDF file
%
% input :
% nc0 --- nc0 struct
% |---name
% |---dims
% |---name
% |---length
% |---atts
% |---name
% |---value
% |---vars
% |---name
% |---type
% |---dims
% |---atts
% |---name
% |---value
%
%
% output :
% \
%
% Siqi Li, SMAST
% 2022-04-06
%
% Updates:
%
%==========================================================================
function nc_def_header0(nc0)
fin = nc0.name;
ncid = nc_create(fin);
nc_close(ncid);
% Define dimensions
for i = 1 : length(nc0.dims)
nc_def_dim(fin, nc0.dims(i).name, nc0.dims(i).length);
end
% Define global attributes
if isfield(nc0, 'atts')
for i = 1 : length(nc0.atts)
nc_def_att(fin, [], nc0.atts(i).name, nc0.atts(i).value);
end
end
% Define variables
for i = 1 : length(nc0.vars)
nc_def_var(fin, nc0.vars(i).name, nc0.vars(i).type, nc0.vars(i).dims);
if isfield(nc0.vars(i), 'atts')
for j = 1 : length(nc0.vars(i).atts)
nc_def_att(fin, nc0.vars(i).name, nc0.vars(i).atts(j).name, nc0.vars(i).atts(j).value);
end
end
end
end