1 | function [var_id,counter] = structtonc(ncid,fieldname,field,depth,var_id,counter,step);
|
---|
2 | %STRUCTTONC- fill nc file with structure fields
|
---|
3 | %
|
---|
4 | % WARNING: Do not use this function, this function is called
|
---|
5 | % by netcdf(model);
|
---|
6 | %
|
---|
7 |
|
---|
8 | %update counter
|
---|
9 | counter = counter+1;
|
---|
10 |
|
---|
11 | %Check that field is not empty
|
---|
12 | if isempty(field),
|
---|
13 | if(step==1), disp(['skipping ' fieldname ' (empty)...']); end
|
---|
14 | return;
|
---|
15 | end
|
---|
16 |
|
---|
17 | %Double scalar
|
---|
18 | if isa(field,'double') & numel(field)==1,
|
---|
19 | if step==1,
|
---|
20 | var_id(counter) = netcdf.defVar(ncid,fieldname,'NC_DOUBLE',[]);
|
---|
21 | else
|
---|
22 | netcdf.putVar(ncid,var_id(counter),field);
|
---|
23 | end
|
---|
24 |
|
---|
25 | %Double vector
|
---|
26 | elseif isa(field,'double') & size(field,2)==1,
|
---|
27 | if step==1,
|
---|
28 | dim_id = netcdf.defDim(ncid,[fieldname '_size1'],size(field,1));
|
---|
29 | var_id(counter) = netcdf.defVar(ncid,fieldname,'NC_DOUBLE',dim_id);
|
---|
30 | else
|
---|
31 | netcdf.putVar(ncid,var_id(counter),field);
|
---|
32 | end
|
---|
33 |
|
---|
34 | %double matrix
|
---|
35 | elseif isa(field,'double') & size(field,2)>1,
|
---|
36 | if step==1,
|
---|
37 | dim1_id = netcdf.defDim(ncid,[fieldname '_size1'],size(field,1));
|
---|
38 | dim2_id = netcdf.defDim(ncid,[fieldname '_size2'],size(field,2));
|
---|
39 | var_id(counter) = netcdf.defVar(ncid,fieldname,'NC_DOUBLE',[dim2_id dim1_id]);
|
---|
40 | else
|
---|
41 | netcdf.putVar(ncid,var_id(counter),transpose(field));
|
---|
42 | end
|
---|
43 |
|
---|
44 | %string
|
---|
45 | elseif isa(field,'char')
|
---|
46 | if step==1,
|
---|
47 | dim_id = netcdf.defDim(ncid,[fieldname '_size1'],numel(field));
|
---|
48 | var_id(counter) = netcdf.defVar(ncid,fieldname,'NC_CHAR',dim_id);
|
---|
49 | else
|
---|
50 | netcdf.putVar(ncid,var_id(counter),transpose(field));
|
---|
51 | end
|
---|
52 |
|
---|
53 | %Boolean of size 1
|
---|
54 | elseif isa(field,'logical') & numel(field)==1,
|
---|
55 | if step==1,
|
---|
56 | var_id(counter) = netcdf.defVar(ncid,fieldname,'NC_BYTE',[]);
|
---|
57 | else
|
---|
58 | netcdf.putVar(ncid,var_id(counter),int8(field));
|
---|
59 | end
|
---|
60 |
|
---|
61 | %Structures
|
---|
62 | elseif isa(field,'struct'),
|
---|
63 | subfields = fields(field);
|
---|
64 | sublength = numel(field);
|
---|
65 | if sublength==1,
|
---|
66 | for i=1:length(subfields),
|
---|
67 | [var_id,counter] = structtonc(ncid,[fieldname '.' subfields{i}],field.(subfields{i}),depth+1,var_id,counter,step);
|
---|
68 | end
|
---|
69 | else
|
---|
70 | for n=1:sublength,
|
---|
71 | for i=1:length(subfields),
|
---|
72 | [var_id,counter] = structtonc(ncid,[fieldname '.' subfields{i} '(' num2str(n) ')'],field(n).(subfields{i}),depth+1,var_id,counter,step);
|
---|
73 | end
|
---|
74 | end
|
---|
75 | end
|
---|
76 |
|
---|
77 | %Cell
|
---|
78 | elseif isa(field,'cell'),
|
---|
79 | for i=1:length(field),
|
---|
80 | [var_id,counter] = structtonc(ncid,[fieldname '{' num2str(i) '}'],field{i},depth+1,var_id,counter,step);
|
---|
81 | end
|
---|
82 |
|
---|
83 | %Objects
|
---|
84 | elseif isobject(field),
|
---|
85 | %First we need to keep track of the object class name
|
---|
86 | [var_id,counter] = structtonc(ncid,[fieldname '_class'],class(field),depth,var_id,counter,step);
|
---|
87 | subfields = fields(field);
|
---|
88 | for i=1:length(subfields),
|
---|
89 | [var_id,counter] = structtonc(ncid,[fieldname '.' subfields{i}],field.(subfields{i}),depth+1,var_id,counter,step);
|
---|
90 | end
|
---|
91 | else
|
---|
92 | disp(['skipping ' fieldname ' (format not supported)...']);
|
---|
93 | end
|
---|