-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuvSiteCodeCoord.m
106 lines (92 loc) · 3.96 KB
/
tuvSiteCodeCoord.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
%% tuvSiteCodeCoord.m
% This function retrieves the site codes and coordinates from the header of
% Codar tuv files.
% INPUT:
% header: header of the Codar ruv or tuv file.
% LLUVSpec: string containing the LLUVSpec version.
% OUTPUT:
% sCC_err: error flag (0 = correct, 1 = error)
% siteCodes: string array containing the site codes
% siteLat: array containing the site codes
% siteLon: array containing the site codes
% Author: Lorenzo Corgnati
% Date: July 11, 2018
% E-mail: lorenzo.corgnati@sp.ismar.cnr.it
%%
function [sCC_err,siteCodes,siteLat,siteLon] = tuvSiteCodeCoord(header,LLUVSpec)
disp(['[' datestr(now) '] - - ' 'tuvSiteCodeCoord.m started.']);
sCC_err = 0;
warning('off', 'all');
try
% Retrieve information from header
if(str2double(LLUVSpec)<1.17)
for header_idx=1:length(header)
splitLine = regexp(header{header_idx}, ' ', 'split');
% Retrieve site codes and coordinates
if(strcmp(splitLine{1}, '%SiteSource:'))
SiteSource = strrep(header{header_idx}(length('%SiteSource:')+2:length(header{header_idx})), '"', '');
siteCellArray = strsplit(SiteSource);
siteCodes(str2double(siteCellArray{2}),:) = siteCellArray{3};
siteLat(str2double(siteCellArray{2})) = str2double(siteCellArray{4});
siteLon(str2double(siteCellArray{2})) = str2double(siteCellArray{5});
end
end
else
for header_idx=1:length(header)
splitLine = regexp(header{header_idx}, ' ', 'split');
% Retrieve MRGS (merge sources) table structure
if(strcmp(splitLine{1}, '%TableType:'))
if(strcmp(splitLine{2}, 'MRGS'))
while(~strcmp(splitLine{1}, '%TableColumnTypes:'))
header_idx = header_idx + 1;
splitLine = regexp(header{header_idx}, ' ', 'split');
end
break
end
end
end
TableColumnTypes = strrep(header{header_idx}(length('%TableColumnTypes:')+2:length(header{header_idx})), '"', '');
tableFields = strsplit(TableColumnTypes);
% Retrieves fields indices
% IndexC = strfind(tableFields, 'SNDX');
% Index = find(not(cellfun('isempty', IndexC)));
CodeC = strfind(tableFields, 'SITE');
Code = find(not(cellfun('isempty', CodeC)));
LatC = strfind(tableFields, 'OLAT');
Lat = find(not(cellfun('isempty', LatC)));
LonC = strfind(tableFields, 'OLON');
Lon = find(not(cellfun('isempty', LonC)));
% Find the MRGS table
while(~strcmp(splitLine{1}, '%TableStart:'))
header_idx = header_idx + 1;
splitLine = regexp(header{header_idx}, ' ', 'split');
end
header_idx = header_idx + 1;
splitLine = regexp(header{header_idx}, ' ', 'split');
while(strcmp(splitLine{1}, '%%'))
splitLine = regexp(header{header_idx}, ' ', 'split');
header_idx = header_idx + 1;
end
site_idx = 0;
while(~strcmp(splitLine{1}, '%TableEnd:'))
site_idx = site_idx + 1;
% Remove empty cells and cells containing '%'
mrgsRow = splitLine(~cellfun('isempty',splitLine));
mrgsRow(1)=[];
% Build site codes and coordinates arrays
tmpCode = mrgsRow{Code};
siteCodes(site_idx,:) = strrep(tmpCode,'"','');
siteLat(site_idx) = str2double(mrgsRow{Lat});
siteLon(site_idx) = str2double(mrgsRow{Lon});
splitLine = regexp(header{header_idx}, ' ', 'split');
header_idx = header_idx + 1;
end
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
sCC_err = 1;
end
if(sCC_err==0)
disp(['[' datestr(now) '] - - ' 'tuvSiteCodeCoord.m successfully executed.']);
end
return