-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_BSRN_LR0100.m
113 lines (94 loc) · 3.43 KB
/
read_BSRN_LR0100.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
function [ok,ID,geo,dates,data,col] = read_BSRN_LR0100(filename)
%READ_BSRN_LR0100 Reads the monthly files containing the BSRN LR0100
% INPUT:
% filename: Name of the file with the whole path.
%
% OUTPUT:
% ok: ok = 1, not ok = 0
% ID: BSRN station ID
% geo: Structure
% geo.lat latitude (decimal degree)
% geo.lon longitude (decimal degree)
% geo.alt altitude (meters)
% dates: a datetime array with the data dates
% data: a numerical matrix with the data
% col: Structure
% col.GHI: GHI column position
% col.DHI: DHI column position
% col.DNI: DNI column position
% col.LWD: LWD column position
%
% - L. Ramírez (May 2015)
% - F. Mendoza (February 2017) Update
% Open an ASCII file delimited by tabs with 1 headers line
my_data = importdata(filename,'\t', 1);
% my_data.textdata = header and text data (station and date)
% my_data.data = matrix with numerical data
% Check if textdata and data exist
is_data = isfield(my_data, 'data');
is_text = isfield(my_data, 'textdata');
if (is_data==1 || is_text==1) % If both fields exist
ok = 1;
num_cols = length(my_data.textdata(1,:));
%----------------------------------------------------------------------
% WORKING WITH my_data.textdata
% output: ID (station)
% dates (vector with the date values in numeric format)
% col (struct with the column numbers of radiation variables)
%----------------------------------------------------------------------
% First column => station ID (All rows have the same value, only 1 is needed)
ID = my_data.textdata(2,1);
% Second column => dates vector (the whole vector is needed, not the header)
Date = my_data.textdata(2:end,2);
% Convert cell to datetime format for output
dates = datetime(Date,'InputFormat','yyyy-MM-dd''T''HH:mm');
% Data headers
% 1:ID; 2:Date; 3:Lat; 4:Lon; 5:Hei;
data_headers = cell(num_cols-5,1); % Preallocate cell
for i = 6:num_cols
data_headers{i-5,1} = my_data.textdata(1,i);
end
% Finding the columns of each variable
col.GHI = NaN;
col.DHI = NaN;
col.DNI = NaN;
col.LWD = NaN;
for i = 1:length(data_headers(:,1))
temp = char(data_headers{i,1});
is_GHI = strfind(temp, '(GLOBAL) radiation [W/m**2]');
if is_GHI>0
col.GHI = i;
end
is_DHI = strfind(temp, 'Diffuse radiation [W/m**2]');
if is_DHI>0
col.DHI = i;
end
is_DNI = strfind(temp, 'Direct radiation [W/m**2]');
if is_DNI>0
col.DNI = i;
end
is_LWD = strfind(temp, 'Long-wave downward radiation [W/m**2]');
if is_LWD>0
col.LWD = i;
end
end
%----------------------------------------------------------------------
% WORKING WITH my_data.data
% output: geo.lat (latitude)
% geo.lon (longitude)
% geo.alt (altitude)
% data (matrix of output data)
%----------------------------------------------------------------------
geo.lat = my_data.data(1,1); % Latitude
geo.lon = my_data.data(1,2); % Longitude
geo.alt = my_data.data(1,3); % Altitude
data = my_data.data(:,4:end); % Data
else % If one of the fields does not exist
ok = 0;
ID = NaN;
geo = NaN;
dates = NaN;
data = NaN;
col = NaN;
end
end