Newer
Older

jacques.grelet_ird.fr
committed
function write(self, varargin)
% write dynaload memory structure to ASCII CSV file (Comma Separated Value)
% nc is a dynaload objet in memory load from xls file. for example:
% >> nc = dynaload('test.xls');

jacques.grelet_ird.fr
committed
% >> nc

jacques.grelet_ird.fr
committed
%
% file: test.xls
%
% DIMENSIONS hashtable
% VARIABLES hashtable
% ATTRIBUTES hashtable

jacques.grelet_ird.fr
committed
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
%
% >> s = nc.VARIABLES.SSTP
%
% s =
% code: 'SSTP'
% dimension: {'DAYD'}
% nctype: 'float'
% long_name: 'SEA SURFACE TEMPERATURE'
% standard_name: 'surface temperature'
% units: 'degrees Celsius'
% conventions: ''
% valid_min: -1.5000
% valid_max: 38
% format: '%6.3lf'
% FillValue_: 99999
% epic_code: []
% axis: ''
% resolution: 1.0000e-003
% comment: [1x99 char]
% missing_value: []
%
% write(nc, 'foo.csv');
%
% >> type foo.csv
%
% datagui_profile_netcdf.csv: 0 members & 0 lines with data; VERSION = 0.000
%
% $DIMENSIONS$
% #;code;value;#
% #;char;integer;#
% #;PROFILE;120;#
% ...
% %
% $VARIABLES$
% #;code;dimension;nctype;long_name;standard_name;units;conventions;valid_min;valid_max;format;FillValue_;epic_code;axis;resolution;comment;missing_value;#
% #;char;cell;char;char;char;char;char;double;double;char;double;double;char;double;char;double;#
% #;REFERENCE_DATE_TIME;'STRING14';char;REFERENCE DATE TIME FOR JULIAN DAYS;reference date time;;yyyymmddhhmmss;;;;;;;;Reference date for julian days origin;;#
% ...
%
% $Id$
% slip filename into path, name & extension
% -----------------------------------------
[path, name, ext] = fileparts(self.file);
% if a second arg is a file name
% -----------------------------
if isa(varargin{1}, 'char')
path_ = fileparts(varargin{1});
if isempty(path_)
self.file = fullfile(path,varargin{1});
else
self.file = varargin{1};
end
elseif strcmpi(ext, '.xls')
% otherwise change xls extension to csv
% -------------------------------------
[path, file, ext] = fileparts(regexprep(self.file, '.xls', '.csv'));
self.file = strcat(path, file, ext);
end
% open csv file
% ---------------
self.fid = fopen(self.file, 'wt');
if self.fid == -1
error(['Can''t open file: ' self.file ' in write mode'])
end
% display information on command window
% -------------------------------------
fprintf('Write data file: %s\n', self.file);
% write comment information
% -------------------------
fprintf( self.fid, '%% %s: %d members & %d lines with data; VERSION = %5.3f\n',...
self.file, 0, 0, 0) ;
fprintf( self.fid, '%% $Id$\n%%\n');
% write information
% ------------------------
for k = keys(self)
fprintf( self.fid, '%%\n$%s$\n', char(k));
% write header
lk = keys(get(self, k));
s = get(get(self, k), lk{1});
% initialise str
% --------------
str = '#;'; str2 = '#;';
for m = fieldnames(s)'
str = strcat(str, sprintf('%s;', char(m)));
value = s.(char(m));
if isa(value,'logical')
str2 = strcat(str2, 'logical;');
elseif isa(value,'char')
str2 = strcat(str2, 'char;');
elseif isa(value,'integer')
str2 = strcat(str2, 'integer;');
elseif isa(value,'single')
str2 = strcat(str2, 'float;');
elseif isa(value,'double')
str2 = strcat(str2, 'double;');
elseif iscell(value)
str2 = strcat(str2, 'cell;');
else
error('dynaload.write: undefine %s value for member %s', ....
char(value), m);
end
end
str = strcat(str, '#'); str2 = strcat(str2, '#');
fprintf( self.fid, '%s\n', str); fprintf( self.fid, '%s\n', str2);
% write data lines
for nk = keys(get(self, k))
s = get(get(self, k), nk);
str = '#;';
for m = fieldnames(s)'
value = s.(char(m));
if islogical(value)
format = '%d';
elseif iscell(value)
format = '%s';
elseif isa(value,'char')
format = '%s';
% else isa(value,'uint8')
% format = '%u';
elseif isa(value,'integer')
format = '%d';
% format %g is used for scientific format 1e36 and %8.8g to write 99999.999
elseif isa(value,'single')
format = '%8.8g';
elseif isa(value,'double')
format = '%8.8g';
else
error('dynaload.write: undefine %s value for member %s', ....
char(value), m);
end
if isempty(value)
str = strcat(str, ';');
else
if iscell(value)
% if iscell, write 'val', or 'val1','val2', etc....
for c = value
% if it is last cell, add ; separator
if strcmp(c, value{size(value,2)})
str = strcat(str, sprintf('''%s'';', char(c)));
else
% add comma separator
str = strcat(str, sprintf('''%s'',', char(c)));
end
end
else
% use strtrim to remove blank (format %8.8g)
% ------------------------------------------
str = strcat(str, strtrim(sprintf(strcat(format, ';'), value)));
end
end
end
str = strcat(str, '#');
fprintf( self.fid, '%s\n', str);
end
end
% close file
% ----------
fclose(self.fid);
end