forked from flozabel/Teddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWFDE5Factors_LST.m
342 lines (311 loc) · 13.1 KB
/
WFDE5Factors_LST.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
341
342
% By Florian Zabel, University of Basel, Switzerland (2024)
% Contact: florian.zabel@unibas.ch
function WFDE5Factors_LST(parameters_out,path_wfde5,outdir,startyear,endyear)
units_out=[];
parameters_wfde5=[];
if(find(contains(parameters_out,'tas','IgnoreCase',true)>=1))
units_out=[units_out,{'K'}];
parameters_wfde5=[parameters_wfde5,{'Tair'}];
end
if(find(contains(parameters_out,'hurs','IgnoreCase',true)>=1))
units_out=[units_out,{'%'}];
parameters_wfde5=[parameters_wfde5,{'Qair'}];
end
if(find(contains(parameters_out,'rsds','IgnoreCase',true)>=1))
units_out=[units_out,{'W m-2'}];
parameters_wfde5=[parameters_wfde5,{'SWdown'}];
end
if(find(contains(parameters_out,'rlds','IgnoreCase',true)>=1))
units_out=[units_out,{'W m-2'}];
parameters_wfde5=[parameters_wfde5,{'LWdown'}];
end
if(find(contains(parameters_out,'ps','IgnoreCase',true)>=1))
units_out=[units_out,{'hPa'}];
parameters_wfde5=[parameters_wfde5,{'PSurf'}];
end
if(find(contains(parameters_out,'sfcwind','IgnoreCase',true)>=1))
units_out=[units_out,{'m s-1'}];
parameters_wfde5=[parameters_wfde5,{'Wind'}];
end
if(find(contains(parameters_out,'pr','IgnoreCase',true)>=1))
units_out=[units_out,{'mm/h'}];
parameters_wfde5=[parameters_wfde5,{'Rainf'}];
end
nyears=endyear-startyear+1;
load('wfde5_mask.mat');
%local solar time
res=0.5;
lst_offset=local_solar_time(res);
lst_offset=repmat(lst_offset,[360,1]);
%calculate local midnight in utc
midnight=lst_offset(1:300,:)*(-1)+1;
midnight(midnight<=0)=midnight(midnight<=0)+24;
midnight_lst=ones(300,720); %set 0am in local solar time
for parameterloop=1:length(parameters_out)
parameter_wfde5=parameters_wfde5{parameterloop};
parameter_out=parameters_out{parameterloop};
unit_out=units_out{parameterloop};
factor=NaN(300,720,24,366);
amp_prctle=NaN(300,720,4,366,'single');
dailyval=NaN(300,720,nyears,366,'single');
if(strcmpi(parameter_out,'tas')==1)
dailyval_min=NaN(300,720,nyears,366,'single');
dailyval_max=NaN(300,720,nyears,366,'single');
end
for doy=1:366
disp(['DOY: ',num2str(doy)]);
data_wfde5=NaN(360,720,24,nyears);
if(strcmpi(parameter_out,'pr')==1)
data_wfde5_snowf=NaN(360,720,24);
end
if(strcmpi(parameter_out,'hurs')==1)
data_wfde5_t=NaN(360,720,24,nyears);
data_wfde5_p=NaN(360,720,24,nyears);
end
n=1;
for year=startyear:endyear
Month=month(doy); %month
DayMonth=day(doy); %day of month
band=DayMonth*24-23;
if(leap_year(year)==0 && Month==2 && DayMonth==29)
n=n+1;
continue
end
data=NaN(360,720,24);
data_compound=NaN(360,720,48);
cdir=[path_wfde5,filesep,parameter_wfde5,filesep];
filelist = dir([cdir,'*_',num2str(year),num2str(Month,'%.2i'),'*.nc']);
filename=[filelist.name];
filename=filename(1:length(filename)-3);
disp(['read WFDE5 file ',filename,' for DOY ',num2str(doy)]);
finfo = ncinfo([cdir,filename,'.nc']);
varname=finfo.Variables(1,4).Name;
x=finfo.Dimensions(1,find(strcmpi({finfo.Dimensions.Name},'lon')==1)).Length;
y=finfo.Dimensions(1,find(strcmpi({finfo.Dimensions.Name},'lat')==1)).Length;
z=finfo.Dimensions(1,find(strcmpi({finfo.Dimensions.Name},'time')==1)).Length;
vartime='time';
times = ncread([cdir,filename,'.nc'],vartime);
times=times/24;
timeoffset=datenum(['01.01.',num2str(1900),' 00:00:00'],'dd.mm.yyyy HH:MM:SS');
formatOut='dd.mm.yyyy HH:MM:SS';
times=double(times)+timeoffset;
year_nc=str2double(datestr(times(band),'yyyy'));
month_nc=str2double(datestr(times(band),'mm'));
day_nc=str2double(datestr(times(band),'dd'));
%read netcdf file for doy+-12h
previousmonth=Month-1;
if(previousmonth<1)
previousmonth=12;
previousyear=year-1;
else
previousyear=year;
end
nextmonth=Month+1;
if(nextmonth>12)
nextmonth=1;
nextyear=year+1;
else
nextyear=year;
end
filelist_previous = dir([cdir,'*_',num2str(previousyear),num2str(previousmonth,'%.2i'),'*.nc']);
if(isempty(filelist_previous))
filelist_previous = dir([cdir,'*_',num2str(year),num2str(previousmonth,'%.2i'),'*.nc']);
end
filename_p=[filelist_previous.name];
filelist_next = dir([cdir,'*_',num2str(nextyear),num2str(nextmonth,'%.2i'),'*.nc']);
if(isempty(filelist_next))
filelist_next = dir([cdir,'*_',num2str(year),num2str(nextmonth,'%.2i'),'*.nc']);
end
filename_n=[filelist_next.name];
finfo_p = ncinfo([cdir,filename_p]);
finfo_n = ncinfo([cdir,filename_n]);
z_p=finfo_p.Dimensions(1,find(strcmpi({finfo.Dimensions.Name},'time')==1)).Length;
z_n=finfo_n.Dimensions(1,find(strcmpi({finfo.Dimensions.Name},'time')==1)).Length;
if(band==1)
data_compound(:,:,1:12)=rot90(ncread([cdir,filename_p],varname,[1 1 z_p-12+1],[x y 12]));
else
data_compound(:,:,1:12)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band-12],[x y 12]));
end
data_compound(:,:,13:36)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band],[x y 24]));
if(band+24>z)
data_compound(:,:,37:48)=rot90(ncread([cdir,filename_n],varname,[1 1 1],[x y 12]));
else
data_compound(:,:,37:48)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band+24],[x y 12]));
end
for y=1:360
for x=1:720
for t=1:24
data(y,x,t)=data_compound(y,x,t+12-lst_offset(y,x));
end
end
end
data_wfde5(:,:,:,n)=data;
if(strcmpi(parameter_out,'pr')==1)
filename=strrep(filename,'Rainf','Snowf');
filename_p=strrep(filename_p,'Rainf','Snowf');
filename_n=strrep(filename_n,'Rainf','Snowf');
cdir=strrep(cdir,'Rainf','Snowf');
disp(['read WFDE5 file ',filename,' for DOY ',num2str(doy)]);
finfo = ncinfo([cdir,filename,'.nc']);
varname=finfo.Variables(1,4).Name;
if(band==1)
data_compound(:,:,1:12)=rot90(ncread([cdir,filename_p],varname,[1 1 z_p-12+1],[x y 12]));
else
data_compound(:,:,1:12)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band-12],[x y 12]));
end
data_compound(:,:,13:36)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band],[x y 24]));
if(band+24>z)
data_compound(:,:,37:48)=rot90(ncread([cdir,filename_n],varname,[1 1 1],[x y 12]));
else
data_compound(:,:,37:48)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band+24],[x y 12]));
end
for y=1:360
for x=1:720
for t=1:24
data(y,x,t)=data_compound(y,x,t+12-lst_offset(y,x));
end
end
end
data_wfde5(:,:,:,n)=data_wfde5(:,:,:,n)+data;
end
if(strcmpi(parameter_out,'hurs')==1)
filename=strrep(filename,'Qair','Tair');
filename_p=strrep(filename_p,'Qair','Tair');
filename_n=strrep(filename_n,'Qair','Tair');
cdir=strrep(cdir,'Qair','Tair');
disp(['read WFDE5 file ',filename,' for DOY ',num2str(doy)]);
finfo = ncinfo([cdir,filename,'.nc']);
varname=finfo.Variables(1,4).Name;
if(band==1)
data_compound(:,:,1:12)=rot90(ncread([cdir,filename_p],varname,[1 1 z_p-12+1],[x y 12]));
else
data_compound(:,:,1:12)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band-12],[x y 12]));
end
data_compound(:,:,13:36)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band],[x y 24]));
if(band+24>z)
data_compound(:,:,37:48)=rot90(ncread([cdir,filename_n],varname,[1 1 1],[x y 12]));
else
data_compound(:,:,37:48)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band+24],[x y 12]));
end
for y=1:360
for x=1:720
for t=1:24
data(y,x,t)=data_compound(y,x,t+12-lst_offset(y,x));
end
end
end
data_wfde5_t(:,:,:,n)=data;
filename=strrep(filename,'Tair','PSurf');
filename_p=strrep(filename_p,'Tair','PSurf');
filename_n=strrep(filename_n,'Tair','PSurf');
cdir=strrep(cdir,'Tair','PSurf');
disp(['read WFDE5 file ',filename,' for DOY ',num2str(doy)]);
finfo = ncinfo([cdir,filename,'.nc']);
varname=finfo.Variables(1,4).Name;
if(band==1)
data_compound(:,:,1:12)=rot90(ncread([cdir,filename_p],varname,[1 1 z_p-12+1],[x y 12]));
else
data_compound(:,:,1:12)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band-12],[x y 12]));
end
data_compound(:,:,13:36)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band],[x y 24]));
if(band+24>z)
data_compound(:,:,37:48)=rot90(ncread([cdir,filename_n],varname,[1 1 1],[x y 12]));
else
data_compound(:,:,37:48)=rot90(ncread([cdir,filename,'.nc'],varname,[1 1 band+24],[x y 12]));
end
for y=1:360
for x=1:720
for t=1:24
data(y,x,t)=data_compound(y,x,t+12-lst_offset(y,x));
end
end
end
data_wfde5_p(:,:,:,n)=data;
end
n=n+1;
end %year
%convert units
if strcmpi(parameter_out,'ps')==1
data_wfde5=data_wfde5/100; %convert surface pressure [Pa] to [hPa]
elseif strcmpi(parameter_out,'pr')==1
data_wfde5=data_wfde5*3600; %convert mass flux density to mm/h
elseif(strcmpi(parameter_out,'hurs')==1)
%convert specific humidity to relative humidity [%]
data_wfde5_t=single(data_wfde5_t-273.16); %[K] to [°C]
data_wfde5_p=single(data_wfde5_p/100); %[Pa] to [hPa=mbar]
%es according to Buck equation; t in °C; es in hPa
a=data_wfde5_t;
b=data_wfde5_t;
c=data_wfde5_t;
d=data_wfde5_t;
%over liquid water, T>0°C
a(data_wfde5_t>=0)=6.1121;
b(data_wfde5_t>=0)=18.678;
c(data_wfde5_t>=0)=234.5;
d(data_wfde5_t>=0)=257.14;
%over ice water, T<0°C
a(data_wfde5_t<0)=6.1115;
b(data_wfde5_t<0)=23.036;
c(data_wfde5_t<0)=333.7;
d(data_wfde5_t<0)=279.82;
es_buck=a.*exp((b-(data_wfde5_t./c)).*(data_wfde5_t./(d+data_wfde5_t))); %saturation vapour pressure [hPa]
e = data_wfde5 .* data_wfde5_p ./ (0.378 .* data_wfde5 + 0.622); %vapour pressure [hPa]
rh = e ./ es_buck *100; %relative humidity [%]
rh(rh>100)=100;
rh(rh<0)=0;
data_wfde5=rh;
end
%reduce noice for solar radiation
if(strcmpi(parameter_out,'rsds')==1)
data_wfde5(data_wfde5<0.1)=0;
end
%calculate daily mean/sum values for climate analogue fingerprint method
if(strcmpi(parameter_out,'pr')==1)
dailyval(:,:,:,doy)=squeeze(sum(data_wfde5(1:300,:,:,:),3,'omitnan'));
elseif(strcmpi(parameter_out,'tas')==1)
dailyval(:,:,:,doy)=squeeze(mean(data_wfde5(1:300,:,:,:),3,'omitnan'));
dailyval_min(:,:,:,doy)=squeeze(min(data_wfde5(1:300,:,:,:),[],3,'omitnan'));
dailyval_max(:,:,:,doy)=squeeze(max(data_wfde5(1:300,:,:,:),[],3,'omitnan'));
else
dailyval(:,:,:,doy)=squeeze(mean(data_wfde5(1:300,:,:,:),3,'omitnan'));
end
clear('data_wfde5','data','data_compound','data_wfde5_snowf','data_wfde5_t','data_wfde5_p');
end %doy
if(~exist(outdir))
mkdir(outdir)
end
%write daily sum/mean
load('lat');
lat(301:360)=[];
load('lon');
timestamps=1:366;
var_name='mean';
var_name_long='daily mean value';
unit_ncdf=[unit_out];
time_unit=['day of year (doy) [1-366], with leap day at doy 60'];
comment=[num2str(startyear),'-',num2str(endyear)];
filename=[outdir,filesep,parameter_out,'_WFDE5_mean_doy_',num2str(startyear),'-',num2str(endyear)];
if(strcmpi(parameter_out,'pr')==1)
var_name='sum';
var_name_long='daily sum value';
unit_ncdf=[unit_out];
filename=[outdir,filesep,parameter_out,'_WFDE5_sum_doy_',num2str(startyear),'-',num2str(endyear)];
end
write_netcdf_dailyval(dailyval,lat,lon,timestamps,var_name,var_name_long,unit_ncdf,time_unit,filename,comment); %save as netcdf
clear('dailyval');
%write daily max/min for tas
if(strcmpi(parameter_out,'tas')==1)
var_name='min';
var_name_long='daily min value';
unit_ncdf=[unit_out];
filename=[outdir,filesep,parameter_out,'_WFDE5_min_doy_',num2str(startyear),'-',num2str(endyear)];
write_netcdf_dailyval(dailyval_min,lat,lon,timestamps,var_name,var_name_long,unit_ncdf,time_unit,filename,comment); %save as netcdf
var_name='max';
var_name_long='daily max value';
unit_ncdf=[unit_out];
filename=[outdir,filesep,parameter_out,'_WFDE5_max_doy_',num2str(startyear),'-',num2str(endyear)];
write_netcdf_dailyval(dailyval_max,lat,lon,timestamps,var_name,var_name_long,unit_ncdf,time_unit,filename,comment); %save as netcdf
clear('dailyval_min','dailyval_max');
end
end %parameter
end