-
Notifications
You must be signed in to change notification settings - Fork 1
/
mooringTempRead.m
137 lines (115 loc) · 3.42 KB
/
mooringTempRead.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
function t=mooringTempRead(fname)
% Input:
% fname = a full filename for any raw temperature data file from the
% moorspice moorings
%
% Output:
% t = structure containing a header character array and a data matrix
% There are 4 file types possible for mooring temperature data: cnv, asc,
% csv, dat, and csa. This function takes the given file for a temperature
% time series and produces a numerical matrix of the timeseries data and
% a charater array with the file header.
% Open file for reading as a text file
fid=fopen(fname,'rt');
% Get file type
fext=fname(end-2:end);
% Initialize header cell
header=cell(1);
data=[];
i=1;
% Check which file type
if strcmp(fext,'cnv')
% .cnv files are used for SBE-37 on French moorings
% Make cell array of header strings
str=fgetl(fid);
% Stop when you reach end mark
while ~strcmp(str,'*END*')
if strncmp(str,'# nquan',7)
nvars=str2double(str(end-1:end));
end
header{i,1}=str;
i=i+1;
str=fgetl(fid);
end
header{i,1}=str;
% Make data matrix
data=transpose(fscanf(fid,'%f',[nvars inf]));
elseif strcmp(fext,'asc')
% .asc files are used for SBE-37 on SIO moorings and all SBE-39
% Make cell array of header strings
str=fgetl(fid);
% Stop when you reach end mark
while ~strncmp(str,'start sample number',19)
header{i,1}=str;
i=i+1;
str=fgetl(fid);
end
header{i,1}=str;
% Make data matrix
dataR=textscan(fid,'%s','Delimiter',',');
nvars=find(strncmp('00:',dataR{1},3),1);
dataR=reshape(dataR{1},nvars,[])';
date=char(dataR{:,nvars-1});
time=char(dataR{:,nvars});
dnum=datenum([date time],'dd mmm yyyyHH:MM:SS');
for i=1:nvars-2
data=[data str2num(char(dataR{:,i}))];
end
data=[data dnum];
elseif strcmp(fext,'csv')
% .csv files are used for all SBE-56
% Make cell array of header strings
str=fgetl(fid);
% Stop when you reach end mark
while ~strncmp(str,'"Date"',6) && ~strncmp(str,'"Sample',7)
header{i,1}=str;
i=i+1;
str=fgetl(fid);
end
header{i,1}=str;
% Make data matrix
if strncmp(str,'"Date"',6)
dataR=textscan(fid,'%q %q %q','Delimiter',',');
date=char(dataR{1});
time=char(dataR{2});
dnum=datenum([date time],'yyyy-mm-ddHH:MM:SS');
data=[str2num(char(dataR{3})) dnum];
else
dataR=textscan(fid,'%q %q %q %q','Delimiter',',');
date=char(dataR{2});
time=char(dataR{3});
dnum=datenum([date time],'yyyy-mm-ddHH:MM:SS');
data=[str2num(char(strrep(dataR{4},',','.'))) dnum];
end
elseif strcmp(fext,'csa')
% .csa files are used for all SIOT
% Make cell array of header strings
str=fgetl(fid);
% Stop when you reach end mark
while ~strncmp(str,'deg_C',5)
header{i,1}=str;
i=i+1;
str=fgetl(fid);
end
header{i,1}=str;
% Make data matrix
data=textscan(fid,'%f');
data=data{1};
elseif strcmp(fext,'dat')
% .dat files are used for all RBR instruments
% Make cell array of header strings
str=fgetl(fid);
% Stop when you reach end mark
while ~strcmp(strtrim(str),'Temp')
header{i,1}=str;
i=i+1;
str=fgetl(fid);
end
header{i,1}=str;
% Make data matrix
data=textscan(fid,'%f');
data=data{1};
end
fclose(fid);
t.data=data;
t.header=char(header);