-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmeteoblue.m
169 lines (153 loc) · 4.77 KB
/
meteoblue.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
function meteoblue(diro,xver)
% meteoblue(diro,xver)
%
% For all the METEOBLUE csv files inside of a directory, turns them into MAT
% files and resaves them there. The format of the data is presumed to be
% consistent with 2019 retrievals from the company.
%
% INPUT:
%
% diro A directory string name
% xver 1 Makes a plot of a variable
% 0 Just does the conversion [default]
%
% SEE ALSO: METEOBLUF
%
% Last modified by fjsimons-at-alum.mit.edu, 09/24/2020
% Defaults
defval('xver',0)
defval('diro',fullfile(getenv('ITALY'),'METEOBLUE','csv'))
% Look inside, find them all
fnames=ls2cell(fullfile(diro,'*.csv'));
% The apparent format - twenty-five including TWENTY weather variables
N=25;
% General data format
fmt=sprintf('%s%%f',repmat('%f;',[1 N-1]));
% Loop over all the csv files
for index=1:length(fnames)
% Get file id
fid=fopen(fullfile(diro,fnames{index}));
% Chop the head off by variable extraction and verification
LAT=xmt(fgetl(fid),3,N,2);
LON=xmt(fgetl(fid),3,N,2);
ASL=xmt(fgetl(fid),3,N,2);
% These ones are harder to script
CITY=fgetl(fid);
DOMAIN=fgetl(fid);
LEVEL=fgetl(fid);
% These ones we parse again below
NAME=fgetl(fid);
UNIT=fgetl(fid);
% This one we ignore
AGGREGATION=fgetl(fid);
% Making sure the UTC_OFFSET is zero
UTC_OFFSET=xmt(fgetl(fid),10,N,1);
% Blank line
fgetl(fid);
% These are the real column headers parse below
HEADER=fgetl(fid);
% And now come the data
data=reshape(fscanf(fid,fmt),N,[])';
% Turn them into slightly more practical variables
dt=datetime([data(:,1:5) repmat(0,[size(data,1) 1])],'TimeZone','UTC');
% List other choices you might have had
% T=timezones('Europe');
% Plot it or save it
if xver==0
% Change directory name
[a,b]=fileparts(diro);
if strcmp(b,'csv')
diro2=fullfile(a,'mat');
end
% Save name for file variable
sname=pref(suf(fnames{index},'_'));
% On second thought, leave the mb_
sname=pref(fnames{index});
cname=fullfile(diro2,sprintf('%s.mat',sname));
if exist(cname)~=2
% Save the data as a MAT file
var1=parse(NAME(10:end),';');
var2=parse(UNIT(10:end),';');
var3=parse(HEADER(1:end),';');
% Vertical spacers
hspc=size(var3,1)-size(var1,1);
vsp1=str2mat(repmat(32,hspc,size(var1,2)));
vsp2=str2mat(repmat(32,hspc,size(var2,2)));
var1=[vsp1 ; var1];
var2=[vsp2 ; var2];
% Horizontal spacers
spc1=repmat(' ',size(var1,1),2);
% Table of contents
toc=number([var3 spc1 var1 spc1 var2]);
% Assign structure
msg=sprintf('Created by [email protected] using %s on %s',upper(mfilename),date);
% The below is a way, but really no way to do this, see also RAPIDEYM
% eval(sprintf('%s.%s=%s;',sname,'toc','toc'))
fields={'lon' 'lat' 'asl' 'toc' 'dt' 'data' 'msg'};
values={LON LAT ASL toc dt data msg};
% Use my homegrown powerhouse
defstruct(sname,fields,values)
% Save it
eval(sprintf('save(''%s'',''%s'')',cname,sname))
else
warning(sprintf('%s already existed!',cname))
end
else
% Make a plot of the ith variable
ith=1;
figure(gcf)
clf
ah=krijetem(subnum(2,1));
% The whole data set in UTC
axes(ah(1))
p(1)=plot(dt,data(:,5+ith));
datetick('x')
title(sprintf('Whole range %s',nounder(fnames{index})))
% The last day in LOCAL TIME
dt.TimeZone='Europe/Rome';
axes(ah(2))
% Logical selection
witsj=dt>[dt(end)-days(1)];
% Index of minimum
mindti=min(find(witsj));
% Index of maximum
maxdti=max(find(witsj));
% Make the plot and return the handle
p(2)=plot(dt(witsj),data(witsj,5+ith));
% Add one hour to come to a round number on the axis
% Newer versions of MATLAB support xlim directly in datetime
xels=[dt(mindti) dt(maxdti)+hours(1)];
% Prepare to set the tick marks
xells=xels(1):hours(4):xels(2);
% Set the axis limits
xlim(datenum(xels))
% Set the tickmarks
set(ah(2),'xtick',datenum(xells))
% Format the axis
datetick('x','HH:MM','keeplimits','keepticks')
% Set the title
title(datestr(dt(mindti),1))
end
end
% Conduct an appropriate extr-action
function [xmt,var]=xmt(str,num,N,dorf)
% You could use this to self-extract the variable if you like!
xmt=textscan(str,hmt(num,N,dorf));
var=xmt{1};
xmt=unique(cat(2,xmt{2:end}));
if length(xmt)~=1
error(sprintf('Expecting unique if repeated header %s',var))
end
if strcmp(var,'UTC_OFFSET')
if xmt~=0
error(sprintf('Expecting zero %s',var))
end
end
% Make some appropriate formatting strings
function hmt=hmt(num,N,dorf)
switch dorf
case 1
hmt=sprintf('%%%is;;;;;%s%%d',num,repmat('%d;',[1 N-6]));
case 2
hmt=sprintf('%%%is;;;;;%s%%f',num,repmat('%f;',[1 N-6]));
end