-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmGlobe_convert_GLDAS.m
469 lines (466 loc) · 27.3 KB
/
mGlobe_convert_GLDAS.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
function mGlobe_convert_GLDAS(start_calc,end_calc,model,time_resol,ghc_path,input_path,input_file)
%MGLOBE_CONVERT_GLDAS Download GLDAS/MERRA data
% Convert required hydrological GLDAS/MERRA data, i.e. soil moisture and snow.
% Water store in vegetation is not taken into account.
%
% ASSUMTPION:
% NOAH ... layer 0 == longitude vector
% ... layer 1 == latitude vector
% ... layer 4 == contains 4 soil moisture layers
% ... layer 5 == snow water equivalent
% CLM ... layer 0 == longitude vector
% ... layer 1 == latitude vector
% ... layer 4 == contains 10 soil moisture layers
% ... layer 5 == snow water equivalent
% MOSAIC ... layer 0 == longitude vector
% ... layer 1 == latitude vector
% ... layer 4 == contains 3 soil moisture layers
% ... layer 5 == snow water equivalent
% VIC ... layer 0 == longitude vector
% ... layer 1 == latitude vector
% ... layer 4 == contains 3 soil moisture layers
% ... layer 5 == snow water equivalent
% MERRA ... MAT1NXLND layers: TWLAND, XDim, YDim
% MERRA2 ... M2T1NXLND layers: TWLAND, lon, lat,time
% NOAH v2.1 ... layer 1 == longitude vector
% ... layer 0 == latitude vector
% ... layer 19-22 == contains 4 soil moisture layers
% ... layer 17 == snow water equivalent
%
% INPUT:
% start_calc ... starting time in matlab format (days)
% Example: datenum([2012,1,1,12,0,0]);
% end_calc ... finish time in matlab format (days)
% Example: datenum([2013,1,1,12,0,0]);
% time_resol ... time resolution switcher: 1 == 3 hours, 2 == 6 hours,
% 3 == 12 hours, 4 == 24 hours, 5 == 48 hours, 6 == month.
% Example: 4
% model ... GLDAS model identification:
% 1 ... CLM model (1.0 deg spatial resolution)
% 2 ... MOSAIC model (1.0 deg spatial resolution)
% 3 ... NOAH with 0.25 deg spatial resolution
% 4 ... NOAH with 1.0 deg spatial resolution
% 5 ... VIC model (1.0 deg spatial resolution)
% 6 ... MERRA model (0.667x0.5 deg spatial resolution)
% 7 ... MERRA model (0.667x0.5 deg spatial resolution)
% 8 ... NOAH025 v2.1 with 0.25 deg spatial resolution
% Example: 1
% ghc_path ... output path (string)
% Example: fullfile('GHM','CLM');
% input_path ... input file folder (string)
% Example: fullfile('E','models','CLM');
% input_file ... input file name (string)
% Example: 'GLDAS_CLM10SUBP_3H.A2013001.0000.001.2015184151845.pss.nc';
%
% OUTPUT (saved automatically):
% out_mat ... structure array (several matrices) containing:
% out_mat.lon ... longitude (degrees)
% out_mat.lat ... latitude (degrees)
% out_mat.time ... civil time (UTC)
% out_mat.soilmX ... soil moisture for layer X (kg/m2)
% out_mat.swe ... snow water equivalent (kg/m2)
% out_mat.twland ... Total Land Water Storage (kg/m2)
% out_mat.units ... swe and soilmX units
%
% M.Mikolaj, [email protected]
% v1.0
%% Time setting
[year_s,month_s] = datevec(start_calc); % transform matlab time to civil date
[year_e,month_e] = datevec(end_calc);
if time_resol == 6 % create time for MONTHly data
j = 1;
for year = year_s:year_e
if j == 1
mz = month_s;
else
mz = 1;
end
if year == year_e
mk = month_e;
else
mk = 12;
end
for m = mz:mk
time(j,1) = year;
time(j,2) = m;
j = j + 1;
end
end
time(:,3) = 1;
time(:,7) = datenum(time(:,1),time(:,2),time(:,3));
else % create time for other resolutions
switch time_resol
case 1
time_resol_in_days = 3/24;
case 2
time_resol_in_days = 6/24;
case 3
time_resol_in_days = 12/24;
case 4
time_resol_in_days = 1;
case 5
time_resol_in_days = 2;
end
days = start_calc:time_resol_in_days:end_calc;
time = datevec(days);
if model==6
time(:,5) = 30;
end
time(:,7) = datenum(time(:,1:6));
clear days
end
progres_perc = linspace(1,99,size(time,1));
for i = 1:size(time,1);
check_out = 0;
try
switch model
%% N O A H 0.25 deg
case 3
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:17),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_NOAH025_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%03d.%02d*',input_file(1:22),time(i,1),floor(time(i,7)-datenum(time(i,1),1,1,0,0,0))+1,time(i,4)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_NOAH025SUBP_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,1));
lon_vec = double(netcdf.getVar(ncid,0));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Select data
temp = double(netcdf.getVar(ncid,4));
out_mat.soilm1 = temp(:,:,4)';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = temp(:,:,3)';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = temp(:,:,2)';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.soilm4 = temp(:,:,1)';out_mat.soilm4(out_mat.soilm4>9.999e+19 | out_mat.soilm4<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,5));out_mat.swe = out_mat.swe';
out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
%% N O A H 1 deg
case 4
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:16),time(i,1),time(i,2))); % create file name with wildcard
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_NOAH10_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%03d.%02d*',input_file(1:21),time(i,1),floor(time(i,7)-datenum(time(i,1),1,1,0,0,0))+1,time(i,4))); % create file name with wildcard
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_NOAH10SUBP_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,1));
lon_vec = double(netcdf.getVar(ncid,0));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Select data
temp = double(netcdf.getVar(ncid,4));
out_mat.soilm1 = temp(:,:,4)';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = temp(:,:,3)';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = temp(:,:,2)';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.soilm4 = temp(:,:,1)';out_mat.soilm4(out_mat.soilm4>9.999e+19 | out_mat.soilm4<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,5));out_mat.swe = out_mat.swe';
out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
%% C L M
case 1
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:15),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_CLM10_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%03d.%02d*',input_file(1:20),time(i,1),floor(time(i,7)-datenum(time(i,1),1,1,0,0,0))+1,time(i,4)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_CLM10SUBP_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,1));
lon_vec = double(netcdf.getVar(ncid,0));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Select data
temp = double(netcdf.getVar(ncid,4));
out_mat.soilm1 = temp(:,:,10)';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = temp(:,:,9)';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = temp(:,:,8)';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.soilm4 = temp(:,:,7)';out_mat.soilm4(out_mat.soilm4>9.999e+19 | out_mat.soilm4<0) = 0;
out_mat.soilm5 = temp(:,:,6)';out_mat.soilm5(out_mat.soilm5>9.999e+19 | out_mat.soilm5<0) = 0;
out_mat.soilm6 = temp(:,:,5)';out_mat.soilm6(out_mat.soilm6>9.999e+19 | out_mat.soilm6<0) = 0;
out_mat.soilm7 = temp(:,:,4)';out_mat.soilm7(out_mat.soilm7>9.999e+19 | out_mat.soilm7<0) = 0;
out_mat.soilm8 = temp(:,:,3)';out_mat.soilm8(out_mat.soilm8>9.999e+19 | out_mat.soilm8<0) = 0;
out_mat.soilm9 = temp(:,:,2)';out_mat.soilm9(out_mat.soilm9>9.999e+19 | out_mat.soilm9<0) = 0;
out_mat.soilm10 = temp(:,:,1)';out_mat.soilm10(out_mat.soilm10>9.999e+19 | out_mat.soilm10<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,5));out_mat.swe = out_mat.swe';
out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat');
clear out_mat nazov cfile
%% M O S A I C
case 2
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:15),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_MOS10_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%03d.%02d*',input_file(1:20),time(i,1),floor(time(i,7)-datenum(time(i,1),1,1,0,0,0))+1,time(i,4)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_MOS10SUBP_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,1));
lon_vec = double(netcdf.getVar(ncid,0));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Select data
temp = double(netcdf.getVar(ncid,4));
out_mat.soilm1 = temp(:,:,3)';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = temp(:,:,2)';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = temp(:,:,1)';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,5));out_mat.swe = out_mat.swe';
out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
%% V I C
case 5
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:15),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_VIC10_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%03d.%02d*',input_file(1:16),time(i,1),floor(time(i,7)-datenum(time(i,1),1,1,0,0,0))+1,time(i,4)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS_VIC10_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,1));
lon_vec = double(netcdf.getVar(ncid,0));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Select data
temp = double(netcdf.getVar(ncid,4));
out_mat.soilm1 = temp(:,:,3)';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = temp(:,:,2)';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = temp(:,:,1)';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,5));out_mat.swe = out_mat.swe';
out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
case 6 % MERRA model
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:36),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('MERRA_M_%4d%02d.mat',time(i,1),time(i,2)));
[~,numvars] = netcdf.inq(ncid); % get all variable names
for j = 0:numvars-1 % transform all layers (not only svwlX and sd)!!
name = netcdf.inqVar(ncid,j); % get variable name
switch name
case 'XDim'
lon_vec = double(netcdf.getVar(ncid,j));
case 'YDim'
lat_vec = double(netcdf.getVar(ncid,j));
case 'TWLAND'
out_mat.twland = double(netcdf.getVar(ncid,j));
out_mat.twland = out_mat.twland';
out_mat.twland(out_mat.twland>9.999e+14 | out_mat.twland<0) = 0;
end
end
% Check if longitude vector exist (new Merra data format does not contain XDim variable)
if ~exist('lon_vec','var')
lon_res = 360/size(out_mat.twland,1);
lon_vec = -180:lon_res:180-lon_res/2;
end
% Do the same for latitude
if ~exist('lat_vec','var')
% Do not use linspace for longitude as -180 == 180 whereas -90 ~= 90 (data for pole)
lat_vec = linspace(-90,90,size(out_mat.twland,2));
end
% Create lon/lat matrix
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%02d%02d*',input_file(1:36),time(i,1),time(i,2),time(i,3)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('MERRA_1H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); %
[~,numvars] = netcdf.inq(ncid); % get all variable names
for j = 0:numvars-1 % transform all layers (not only svwlX and sd)!!
name = netcdf.inqVar(ncid,j); % get variable name
switch name
case 'XDim'
lon_vec = double(netcdf.getVar(ncid,j));
case 'YDim'
lat_vec = double(netcdf.getVar(ncid,j));
case 'TWLAND'
temp = double(netcdf.getVar(ncid,j));
out_mat.twland = temp(:,:,time(i,4)+1);
out_mat.twland = out_mat.twland';
out_mat.twland(out_mat.twland>9.999e+14 | out_mat.twland<0) = 0;
end
end
% Check if longitude vector exist (new Merra data format does not contain XDim variable)
if ~exist('lon_vec','var')
lon_res = 360/size(out_mat.twland,2);
lon_vec = -180:lon_res:180-lon_res/2;
end
% Do the same for latitude
if ~exist('lat_vec','var')
% Do not use linspace for longitude as -180 == 180 whereas -90 ~= 90 (data for pole)
lat_vec = linspace(-90,90,size(out_mat.twland,1));
end
% Create lon/lat matrix
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
end
case 7 % MERRA2
switch time_resol
case 6
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:27),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('MERRA2_M_%4d%02d.mat',time(i,1),time(i,2)));
[~,numvars] = netcdf.inq(ncid); % get all variable names
for j = 0:numvars-1 % transform all layers (not only svwlX and sd)!!
name = netcdf.inqVar(ncid,j); % get variable name
switch name
case 'lon'
lon_vec = double(netcdf.getVar(ncid,j));
% take care of rounding error
lon_vec = round(lon_vec*1e+10)./1e+10;
case 'lat'
lat_vec = double(netcdf.getVar(ncid,j));
lat_vec = round(lat_vec*1e+10)./1e+10;
case 'TWLAND'
temp = double(netcdf.getVar(ncid,j));
end
end
out_mat.twland = temp';
out_mat.twland(out_mat.twland>9.999e+14 | out_mat.twland<0) = 0;
% Create lon/lat matrix
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
otherwise
cfile = fullfile(input_path,sprintf('%s%04d%02d%02d*',input_file(1:27),time(i,1),time(i,2),time(i,3)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('MERRA2_1H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); %
[~,numvars] = netcdf.inq(ncid); % get all variable names
for j = 0:numvars-1 % transform all layers (not only svwlX and sd)!!
name = netcdf.inqVar(ncid,j); % get variable name
switch name
case 'lon'
lon_vec = double(netcdf.getVar(ncid,j));
% take care of rounding error
lon_vec = round(lon_vec*1e+10)./1e+10;
case 'lat'
lat_vec = double(netcdf.getVar(ncid,j));
lat_vec = round(lat_vec*1e+10)./1e+10;
case 'TWLAND'
temp = double(netcdf.getVar(ncid,j));
end
end
out_mat.twland = temp(:,:,time(i,4)+1);
out_mat.twland = out_mat.twland';
out_mat.twland(out_mat.twland>9.999e+14 | out_mat.twland<0) = 0;
% Check if longitude vector exist (new Merra data format does not contain XDim variable)
if ~exist('lon_vec','var')
lon_res = 360/size(out_mat.twland,2);
lon_vec = -180:lon_res:180-lon_res/2;
end
% Do the same for latitude
if ~exist('lat_vec','var')
% Do not use linspace for longitude as -180 == 180 whereas -90 ~= 90 (data for pole)
lat_vec = linspace(-90,90,size(out_mat.twland,1));
end
% Create lon/lat matrix
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat')
end
case 8 % GLDAS NOAH025 v2.1
switch time_resol
case 6 % monthly data
cfile = fullfile(input_path,sprintf('%s%04d%02d*',input_file(1:17),time(i,1),time(i,2)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS2_NOAH025_M_%4d%02d.mat',time(i,1),time(i,2)));
otherwise % hourly data
cfile = fullfile(input_path,sprintf('%s%04d%02d%02d.%02d*',input_file(1:18),time(i,1),time(i,2),time(i,3),time(i,4)));
cfile = dir(cfile); % get exact file name without wildcard
ncid = netcdf.open(fullfile(input_path,cfile.name),'NC_NOWRITE'); % use fullfile function as the dir command returns only file name, no path
nazov = fullfile(ghc_path,sprintf('GLDAS2_NOAH025SUBP_3H_%4d%02d%02d_%02d.mat',time(i,1),time(i,2),time(i,3),time(i,4))); % Daily data
end
lat_vec = double(netcdf.getVar(ncid,0));
lon_vec = double(netcdf.getVar(ncid,1));
[out_mat.lon,out_mat.lat] = meshgrid(lon_vec,lat_vec);
% Index for soilm1,soilm2,...SWE
layer_index = [19,20,21,22,17];
% Select data
out_mat.soilm1 = double(netcdf.getVar(ncid,layer_index(1)))';out_mat.soilm1(out_mat.soilm1>9.999e+19 | out_mat.soilm1<0) = 0;
out_mat.soilm2 = double(netcdf.getVar(ncid,layer_index(2)))';out_mat.soilm2(out_mat.soilm2>9.999e+19 | out_mat.soilm2<0) = 0;
out_mat.soilm3 = double(netcdf.getVar(ncid,layer_index(3)))';out_mat.soilm3(out_mat.soilm3>9.999e+19 | out_mat.soilm3<0) = 0;
out_mat.soilm4 = double(netcdf.getVar(ncid,layer_index(4)))';out_mat.soilm4(out_mat.soilm4>9.999e+19 | out_mat.soilm4<0) = 0;
out_mat.swe = double(netcdf.getVar(ncid,layer_index(5)))';out_mat.swe(out_mat.swe>9.999e+19 | out_mat.swe<0) = 0;
out_mat.time = time(i,7);
out_mat.source = cfile;
out_mat.units = 'mm';
netcdf.close(ncid);
save(nazov,'out_mat');
end
catch
check_out = 1;
end
switch check_out
case 0
if size(time,1) > 2
out_message = sprintf('Models: converting GLDAS/MERRA model ... (%3.0f%%)',progres_perc(i)); % create message
else
out_message = sprintf('Models: converting GLDAS/MERRA model ...'); % create message
end
case 1
out_message= sprintf('Models: could not convert data for: %04d%02d%02d_%02d',time(i,1),time(i,2),time(i,3),time(i,4));
fprintf('%s\n',out_message);
end
set(findobj('Tag','text_status'),'String',out_message); drawnow
clear nazov
end