-
Notifications
You must be signed in to change notification settings - Fork 3
/
mark2mat.m
206 lines (186 loc) · 5.52 KB
/
mark2mat.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
function varargout=mark2mat(fname,hord)
% [t,d,h]=MARK2MAT(fname)
%
% Reads, and converts a CSV file from the Arable Mark 2 instrument to a
% MATLAB file including proper date-time variables. The format of the data
% line is according to our knowledge of 'daily' and 'hourly' files, with
% and without external data (wind) sensor. Also for external sensors.
%
%
% INPUT:
%
% fname A complete file name string
% hord 1 'hourly' data file
% 2 'daily' data file
%
% OUTPUT:
%
% t The timestamp as a DATETIME array
% d The data as a STRUCTURE array
% h The header line CELL array
%
% SEE ALSO:
%
% DROP2MAT
%
% EXAMPLE:
%
% mark2mat('demo1')
%
% TESTED ON:
%
% 9.0.0.341360 (R2016a)
% 9.4.0.813654 (R2018a)
%
% Last modified by fjsimons-at-alum.mit.edu, 09/21/2020
% Where do the location etc get unicized? Should do the same for model
if isempty(strfind(fname,'demo'))
% Prepare to save the CSV file as a MAT file
[aa,bb,cc]=fileparts(fname);
ename=sprintf('%s.mat',bb);
if exist(ename)~=2
% Open the file
fid=fopen(fname);
% Read the first few lines as a "header"
for index=1:10
% These are all read in straight
h{index}=fgetl(fid);
end
% Take care of possible disclaimer
if ~isempty(h{10})
% This the empty
h{11}=fgetl(fid);
end
% Force this to be the header, create extra empty
h{12}=fgetl(fid);
% Do the only one that is relevant here - Location
vnames=h{4}; vnames(abs(vnames)==32)='';
% Got to know there are double quotes in there
[v1,v2]=strread(vnames,'%s%q','delimiter',',');
d.(char(v1))=char(v2);
% Other, rejected ways
% d=struct(char(v{1}(5)),a{5})
% d=cellstruct({a{5}},char(v{1}(5)))
% Unit designations
for index=6:9
% Do the only one that is relevant here
vnames=h{index}; vnames(abs(vnames)==32)='';
vnames(abs(vnames)==47)='';
% Got to know there are double quotes in there
[v1,v2]=strread(vnames,'%s%q','delimiter',',');
v1=strcat(v1,'Unit');
d.(char(v1))=char(v2);
end
% Pick out the data variable names
% Replace the underscores with nothing
vnames=h{12}; vnames(abs(vnames)==95)='';
% These are simple parameter value pairs, prefer TEXTSCAN over STRREAD
v=textscan(vnames,'%s','delimiter',',');
% Read the rest as the "data"
sv11=size(v{1},1);
if sv11>=38
% Mark 1 hourly file
fms=sprintf('%s%s%s%s%s',repmat('%s',1,4),repmat('%f',1,30),...
'%s',repmat('%f',1,3));
if sv11==39
% Mark 2 appears to have one more column (and time order switched)
fms=[fms '%s'];
end
if sv11==146
% Mark 2 hourly Sentek file
fms=sprintf('%s%s%s%s%s',repmat('%s',1,4),repmat('%f',1,94),...
'%s',repmat('%f',1,47));
end
if sv11==145
% Mark 2 daily Sentek file
fms=sprintf('%s%s%s%s%s',repmat('%s',1,3),repmat('%f',1,94),...
'%s',repmat('%f',1,47));
end
else
if sv11==28
% Mark 1 daily file
fms=sprintf('%s%s%s%s%s',repmat('%s',1,4),repmat('%f',1,20),...
'%s',repmat('%f',1,3));
end
if sv11==26
% Mark 2 daily file
fms=sprintf('%s%s%s%s%s%s',repmat('%s',1,3),repmat('%f',1,18),...
'%s',repmat('%f',1,3),'%s');
end
end
a=textscan(fid,fms,'Delimiter',',');
% Close the file
fclose(fid);
% One of the first two is the UTC time, this could be switched
utc=find(strcmp(v{1},'utctime'));
if isempty(utc); utc=1; end
% Note that we are going with the UTC times
try
% Convert the ISO8601 time stamps to MATLAB datetime arrays
% by removing the 'Z' and blanking the 'T' (84->32) via an anoymous
% function. Note that putting the DATETIME inside would take longer
convt=@(x) char(abs(x(1:end-1))-[zeros(1,10) 52 zeros(1,8)]);
t=datetime(cellfun(convt,a{utc},'UniformOutput',0));
catch
% A newer, simpler format
t=datetime(a{utc});
end
% Definitely set the time zone
t.TimeZone='UTC';
% Sanity check
disp(sprintf('%s records expected, %i received',...
h{5}(abs(h{5})>=48 & abs(h{5})<=58),length(t)))
isdaily=~isempty(strfind(fname,'daily'));
% The simple tags of device and location
for index=[3:4]-isdaily
eval(sprintf('d.%s=char(a{%i}(1));',char(v{1}(index)),index))
end
% The geographical tags (if it's not an external probe, but never mind)
for index=[5:6]-isdaily
eval(sprintf('d.%s=a{%i};',char(v{1}(index)),index))
end
try
% Need to know these variables ARE there and called 'lat' and 'long'
try
% Need the mapping toolbox
[la,lo]=meanm(d.lat(~isnan(d.lat)),d.long(~isnan(d.long)));
catch
la=nanmean(d.lat);
lo=nanmean(d.long);
end
% And I rename the longitude
d.lat=la;
d.lon=lo;
d=rmfield(d,'long');
end
% All the rest of the data
for index=7:sv11
eval(sprintf('d.%s=a{%i};',char(v{1}(index)),index))
end
% Remove the completely empty numeric columns
fn=fieldnames(d);
for index=1:length(fn)
dn=d.(fn{index});
try
if sum(isnan(dn))==length(dn)
d=rmfield(d,fn{index});
end
end
end
% Save
save(bb,'t','d','h')
else
disp(sprintf('%s: %s existed',upper(mfilename),ename))
load(ename)
end
elseif strcmp(fname,'demo1')
% Supply an existing filename
[t,d]=mark2mat('arable__Guyot_Roof_C003384_hourly_20200823.csv');
if nargout==0
% Make a picture, take your inspiration from DROP2MAT
plot(t,Tair)
end
end
% Optional output
varns={t,d,h};
varargout=varns(1:nargout);