-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpvl_readtmy3.m
340 lines (325 loc) · 16.5 KB
/
pvl_readtmy3.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
function TMYData = pvl_readtmy3(varargin)
% PVL_READTMY3 Read a TMY3 file in to a MATLAB struct
%
% Syntax
% TMYData = pvl_readtmy3()
% TMYData = pvl_readtmy3(FileName)
%
% Description
% Read a TMY3 file and make a struct of the data. Note that values
% contained in the struct are unchanged from the TMY3 file (i.e. units
% are retained). In the case of any discrepencies between this
% documentation and the TMY3 User's Manual ([1]), the TMY3 User's Manual
% takes precedence.
%
% If a FileName is not provided, the user will be prompted to browse to
% an appropriate TMY3 file.
%
% TMY3 format was changed slightly on January 19, 2015 ([2]) to include
% Present weather variables along with changes to other variables.
%
% Input
% FileName - an optional argument which allows the user to select which
% TMY3 format file should be read. A file path may also be necessary if
% the desired TMY3 file is not in the MATLAB working path.
%
% Output
% A struct, TMYData, is provided with the following components. Note
% that for more detailed descriptions of each component, please consult
% the TMY3 User's Manual ([1]), especially tables 1-1 through 1-6. If
% the output size is not specified, it is a 8760x1 vector of type double.
%
% TMYData.SiteID - Site identifier code (USAF number), scalar double
% TMYData.StationName - Station name, 1x1 cell string
% TMYData.StationState - Station state 2 letter designator, 1x1 cell string
% TMYData.SiteTimeZone - Hours from Greenwich, scalar double
% TMYData.SiteLatitude - Latitude in decimal degrees, scalar double
% TMYData.SiteLongitude - Longitude in decimal degrees, scalar double
% TMYData.SiteElevation - Site elevation in meters, scalar double
% TMYData.DateString - Date string in mm/dd/yyyy format, 8760x1 cell string
% TMYData.TimeString - Time string in HH:MM format, local standard time, 8760x1 cell string
% TMYData.DateNumber - Combination of date/time in MATLAB serial date (datenum) format, 8760x1 double
% TMYData.ETR - Extraterrestrial horizontal radiation recv'd during 60 minutes prior to timestamp, Wh/m^2
% TMYData.ETRN - Extraterrestrial normal radiation recv'd during 60 minutes prior to timestamp, Wh/m^2
% TMYData.GHI - Direct and diffuse horizontal radiation recv'd during 60 minutes prior to timestamp, Wh/m^2
% TMYData.GHISource - See [1], Table 1-4
% TMYData.GHIUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.DNI - Amount of direct normal radiation (modeled) recv'd during 60 mintues prior to timestamp, Wh/m^2
% TMYData.DNISource - See [1], Table 1-4
% TMYData.DNIUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.DHI - Amount of diffuse horizontal radiation recv'd during 60 minutes prior to timestamp, Wh/m^2
% TMYData.DHISource - See [1], Table 1-4
% TMYData.DHIUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.GHillum - Avg. total horizontal illuminance recv'd during the 60 minutes prior to timestamp, lx
% TMYData.GHillumSource - See [1], Table 1-4
% TMYData.GHillumUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.DNillum - Avg. direct normal illuminance recv'd during the 60 minutes prior to timestamp, lx
% TMYData.DNillumSource - See [1], Table 1-4
% TMYData.DNillumUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.DHillum - Avg. horizontal diffuse illuminance recv'd during the 60 minutes prior to timestamp, lx
% TMYData.DHillumSource - See [1], Table 1-4
% TMYData.DHillumUncertainty - Uncertainty based on random and bias error estimates - see [2]
% TMYData.Zenithlum - Avg. luminance at the sky's zenith during the 60 minutes prior to timestamp, cd/m^2
% TMYData.ZenithlumSource - See [1], Table 1-4
% TMYData.ZenithlumUncertainty - Uncertainty based on random and bias error estimates - see [1] section 2.10
% TMYData.TotCld - Amount of sky dome covered by clouds or obscuring phenonema at time stamp, tenths of sky
% TMYData.TotCldSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.TotCldUnertainty - See [1], Table 1-6
% TMYData.OpqCld - Amount of sky dome covered by clouds or obscuring phenonema that prevent observing the
% sky at time stamp, tenths of sky
% TMYData.OpqCldSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.OpqCldUncertainty - See [1], Table 1-6
% TMYData.DryBulb - Dry bulb temperature at the time indicated, deg C
% TMYData.DryBulbSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.DryBulbUncertainty - See [1], Table 1-6
% TMYData.DewPoint - Dew-point temperature at the time indicated, deg C
% TMYData.DewPointSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.DewPointUncertainty - See [1], Table 1-6
% TMYData.RHum - Relative humidity at the time indicated, percent
% TMYData.RHumSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.RHumUncertainty - See [1], Table 1-6
% TMYData.Pressure - Station pressure at the time indicated, 1 mbar
% TMYData.PressureSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.PressureUncertainty - See [1], Table 1-6
% TMYData.Wdir - Wind direction at time indicated, degrees from north (360 = north; 0 = undefined,calm)
% TMYData.WdirSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.WdirUncertainty - See [1], Table 1-6
% TMYData.Wspd - Wind speed at the time indicated, meter/second
% TMYData.WspdSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.WspdUncertainty - See [1], Table 1-6
% TMYData.Hvis - Distance to discernable remote objects at time indicated (7777=unlimited), meter
% TMYData.HvisSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.HvisUncertainty - See [1], Table 1-6
% TMYData.CeilHgt - Height of cloud base above local terrain (7777=unlimited), meter
% TMYData.CeilHgtSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.CeilHgtUncertainty - See [1], Table 1-6
% TMYData.Pwat - Total precipitable water contained in a column of unit cross section from
% earth to top of atmosphere, cm
% TMYData.PwatSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.PwatUncertainty - See [1], Table 1-6
% TMYData.AOD - The broadband aerosol optical depth per unit of air mass due to extinction by
% aerosol component of atmosphere, unitless
% TMYData.AODSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.AODUncertainty - See [1], Table 1-6
% TMYData.Alb - The ratio of reflected solar irradiance to global horizontal irradiance, unitless
% TMYData.AlbSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.AlbUncertainty - See [1], Table 1-6
% TMYData.Lprecipdepth - The amount of liquid precipitation observed at indicated time for the period indicated
% in the liquid precipitation quantity field, millimeter
% TMYData.Lprecipquantity - The period of accumulation for the liquid precipitation depth field, hour
% TMYData.LprecipSource - See [1], Table 1-5, 8760x1 cell array of strings
% TMYData.LprecipUncertainty - See [1], Table 1-6
% TMYData.PresWth - Present weather code, see [2]. Only available in
% TMY3 files created after the January 2015 TMY3 update.
% TMYData.PresWthSource - Present weather code source, see [2]. Only
% available in TMY3 files created after the Jan. 2015 TMY3 update.
% TMYData.PresWthUncertainty - Present weather code uncertainty, see
% [2]. Only available in TMY3 files after the Jan. 2015 update.
%
% Reference
% [1] Wilcox, S and Marion, W., 2008. Users Manual for TMY3 Data Sets,
% NREL/TP-581-43156, National Renewable Energy Laboratory. Available at
% <http://www.nrel.gov/docs/fy08osti/43156.pdf>.
%
% [2] National Solar Radiation Data Base, 1991-2005 Update: Typical
% Meteorological Year 3.
% http://rredc.nrel.gov/solar/old_data/nsrdb/1991-2005/tmy3/ retrieved June
% 6, 2016.
%
%
% See also
% DATEVEC PVL_MAKELOCATIONSTRUCT PVL_MAKETIMESTRUCT PVL_READTMY2
if (size(varargin) == 0)
[FileNameAndExt, FilePath]=uigetfile({ '*.csv;*.tmy3' , 'TMY3 Files (*.csv, *.tmy3)';'*.*', 'All Files (*.*)'}, 'Select a TMY3 File');
FilePathAndNameAndExt = [FilePath filesep FileNameAndExt];
elseif size(varargin) == 1
FilePathAndNameAndExt = varargin{1};
[FilePath, FileName, FileExt] = fileparts(FilePathAndNameAndExt);
FileNameAndExt = [FileName FileExt];
end
p = inputParser;
p.addRequired('FilePathAndNameAndExt', @(x) ischar(x));
p.addRequired('FilePath', @(x) ischar(x));
p.parse(FilePathAndNameAndExt, FilePath)
FileID = fopen(FilePathAndNameAndExt);
Header1 = textscan(FileID, '%f%q%q%f%f%f%f', 1,'Delimiter',',');
Header2 = textscan(FileID, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s',1,'Delimiter',',');
DataLines = textscan(FileID, '%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%f%s%f',8760,'Delimiter',',');
ST = fclose(FileID);
%% Determine if the file is from pre-January 19, 2015 or post-20150119
if ~contains(char(DataLines{1,1}{1,1}), 'PresWth')
% The string 'PresWth' was not found in DataLines{1,1}{1,1}, so we have a
% pre-2015 TMY3 file
TMYData = ParsePre2015TMY(DataLines);
else
% The string 'PresWth' was found in DataLines{1,1}{1,1}, so we have a
% post-2015 TMY3 file. We'll need to re-import the file to accomodate
% the additional columns.
FileID = fopen(FilePathAndNameAndExt);
Header1 = textscan(FileID, '%f%q%q%f%f%f%f', 1,'Delimiter',',');
Header2 = textscan(FileID, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s',1,'Delimiter',',');
DataLines = textscan(FileID, '%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%s%f%f%f%s%f%f%s%f',8760,'Delimiter',',');
ST = fclose(FileID);
TMYData = ParsePost2015TMY(DataLines);
end
% Parse out the header information since it is common to both formats
TMYData.SiteID = Header1{1};
TMYData.StationName = Header1{2};
TMYData.StationState = Header1{3};
TMYData.SiteTimeZone = Header1{4};
TMYData.SiteLatitude = Header1{5};
TMYData.SiteLongitude = Header1{6};
TMYData.SiteElevation = Header1{7};
end
function TMYData = ParsePre2015TMY(DataLines)
%% Read in the data from the struct created from the textscan for TMY3
% This fuction will read pre-2015 TMY3 files and sort them into column
% vectors. A different format is used for TMY3 files after the January 2015
% update, and thus requires a different function.
TMYData.DateString = DataLines{1};
TMYData.TimeString = DataLines{2};
TMYData.ETR = DataLines{3};
TMYData.ETRN = DataLines{4};
TMYData.GHI = DataLines{5};
TMYData.GHISource = DataLines{6};
TMYData.GHIUncertainty = DataLines{7};
TMYData.DNI = DataLines{8};
TMYData.DNISource = DataLines{9};
TMYData.DNIUncertainty = DataLines{10};
TMYData.DHI = DataLines{11};
TMYData.DHISource = DataLines{12};
TMYData.DHIUncertainty = DataLines{13};
TMYData.GHillum = DataLines{14};
TMYData.GHillumSource = DataLines{15};
TMYData.GHillumUncertainty = DataLines{16};
TMYData.DNillum = DataLines{17};
TMYData.DNillumSource = DataLines{18};
TMYData.DNillumUncertainty = DataLines{19};
TMYData.DHillum = DataLines{20};
TMYData.DHillumSource = DataLines{21};
TMYData.DHillumUncertainty = DataLines{22};
TMYData.Zenithlum = DataLines{23};
TMYData.ZenithlumSource = DataLines{24};
TMYData.ZenithlumUncertainty = DataLines{25};
TMYData.TotCld = DataLines{26};
TMYData.TotCldSource = DataLines{27};
TMYData.TotCldUnertainty = DataLines{28};
TMYData.OpqCld = DataLines{29};
TMYData.OpqCldSource = DataLines{30};
TMYData.OpqCldUncertainty = DataLines{31};
TMYData.DryBulb = DataLines{32};
TMYData.DryBulbSource = DataLines{33};
TMYData.DryBulbUncertainty = DataLines{34};
TMYData.DewPoint = DataLines{35};
TMYData.DewPointSource = DataLines{36};
TMYData.DewPointUncertainty = DataLines{37};
TMYData.RHum = DataLines{38};
TMYData.RHumSource = DataLines{39};
TMYData.RHumUncertainty = DataLines{40};
TMYData.Pressure = DataLines{41};
TMYData.PressureSource = DataLines{42};
TMYData.PressureUncertainty = DataLines{43};
TMYData.Wdir = DataLines{44};
TMYData.WdirSource = DataLines{45};
TMYData.WdirUncertainty = DataLines{46};
TMYData.Wspd = DataLines{47};
TMYData.WspdSource = DataLines{48};
TMYData.WspdUncertainty = DataLines{49};
TMYData.Hvis = DataLines{50};
TMYData.HvisSource = DataLines{51};
TMYData.HvisUncertainty = DataLines{52};
TMYData.CeilHgt = DataLines{53};
TMYData.CeilHgtSource = DataLines{54};
TMYData.CeilHgtUncertainty = DataLines{55};
TMYData.Pwat = DataLines{56};
TMYData.PwatSource = DataLines{57};
TMYData.PwatUncertainty = DataLines{58};
TMYData.AOD = DataLines{59};
TMYData.AODSource = DataLines{60};
TMYData.AODUncertainty = DataLines{61};
TMYData.Alb = DataLines{62};
TMYData.AlbSource = DataLines{63};
TMYData.AlbUncertainty = DataLines{64};
TMYData.Lprecipdepth = DataLines{65};
TMYData.Lprecipquantity = DataLines{66};
TMYData.LprecipSource = DataLines{67};
TMYData.LprecipUncertainty = DataLines{68};
%% Create a MATLAB datenum from the string date and time
TMYData.DateNumber = datenum(strcat(TMYData.DateString,'-',TMYData.TimeString),'mm/dd/yyyy-HH:MM'); %MATLAB datenum format
end
function TMYData = ParsePost2015TMY(DataLines)
%% Read in the data from the struct created from the textscan
TMYData.DateString = DataLines{1};
TMYData.TimeString = DataLines{2};
TMYData.ETR = DataLines{3};
TMYData.ETRN = DataLines{4};
TMYData.GHI = DataLines{5};
TMYData.GHISource = DataLines{6};
TMYData.GHIUncertainty = DataLines{7};
TMYData.DNI = DataLines{8};
TMYData.DNISource = DataLines{9};
TMYData.DNIUncertainty = DataLines{10};
TMYData.DHI = DataLines{11};
TMYData.DHISource = DataLines{12};
TMYData.DHIUncertainty = DataLines{13};
TMYData.GHillum = DataLines{14};
TMYData.GHillumSource = DataLines{15};
TMYData.GHillumUncertainty = DataLines{16};
TMYData.DNillum = DataLines{17};
TMYData.DNillumSource = DataLines{18};
TMYData.DNillumUncertainty = DataLines{19};
TMYData.DHillum = DataLines{20};
TMYData.DHillumSource = DataLines{21};
TMYData.DHillumUncertainty = DataLines{22};
TMYData.Zenithlum = DataLines{23};
TMYData.ZenithlumSource = DataLines{24};
TMYData.ZenithlumUncertainty = DataLines{25};
TMYData.TotCld = DataLines{26};
TMYData.TotCldSource = DataLines{27};
TMYData.TotCldUnertainty = DataLines{28};
TMYData.OpqCld = DataLines{29};
TMYData.OpqCldSource = DataLines{30};
TMYData.OpqCldUncertainty = DataLines{31};
TMYData.DryBulb = DataLines{32};
TMYData.DryBulbSource = DataLines{33};
TMYData.DryBulbUncertainty = DataLines{34};
TMYData.DewPoint = DataLines{35};
TMYData.DewPointSource = DataLines{36};
TMYData.DewPointUncertainty = DataLines{37};
TMYData.RHum = DataLines{38};
TMYData.RHumSource = DataLines{39};
TMYData.RHumUncertainty = DataLines{40};
TMYData.Pressure = DataLines{41};
TMYData.PressureSource = DataLines{42};
TMYData.PressureUncertainty = DataLines{43};
TMYData.Wdir = DataLines{44};
TMYData.WdirSource = DataLines{45};
TMYData.WdirUncertainty = DataLines{46};
TMYData.Wspd = DataLines{47};
TMYData.WspdSource = DataLines{48};
TMYData.WspdUncertainty = DataLines{49};
TMYData.Hvis = DataLines{50};
TMYData.HvisSource = DataLines{51};
TMYData.HvisUncertainty = DataLines{52};
TMYData.CeilHgt = DataLines{53};
TMYData.CeilHgtSource = DataLines{54};
TMYData.CeilHgtUncertainty = DataLines{55};
TMYData.Pwat = DataLines{56};
TMYData.PwatSource = DataLines{57};
TMYData.PwatUncertainty = DataLines{58};
TMYData.AOD = DataLines{59};
TMYData.AODSource = DataLines{60};
TMYData.AODUncertainty = DataLines{61};
TMYData.Alb = DataLines{62};
TMYData.AlbSource = DataLines{63};
TMYData.AlbUncertainty = DataLines{64};
TMYData.Lprecipdepth = DataLines{65};
TMYData.Lprecipquantity = DataLines{66};
TMYData.LprecipSource = DataLines{67};
TMYData.LprecipUncertainty = DataLines{68};
TMYData.PresWth = DataLines{69};
TMYData.PresWthSource = DataLines{70};
TMYData.PresWthUncertainty = DataLines{71};
%% Create a MATLAB datenum from the string date and time
TMYData.DateNumber = datenum(strcat(TMYData.DateString,'-',TMYData.TimeString),'mm/dd/yyyy-HH:MM'); %MATLAB datenum format
end