-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreaders.py
843 lines (688 loc) · 30.8 KB
/
readers.py
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# libraries
import pandas as pd
from tqdm.auto import tqdm
import numpy as np
import xarray as xr
from datetime import datetime
from netCDF4 import Dataset
import subprocess
import warnings
import os
def landflag(lis_input_file):
"""
Return flag indicating whether a grid cell is land or not
:param str lis_input_file: Path to LIS input file containing LANDMASK variable.
"""
with Dataset(lis_input_file, mode = "r") as f:
landflag = f.variables["LANDMASK"][:,:].data
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
landflag = xr.DataArray(
data = np.array(landflag, dtype = bool),
dims = ["x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
return landflag
def landcover(lis_input_file, majority = True, classification_system = "IGBP"):
"""
Return map with landcover data from the LIS input file
:param str lis_input_file: Path to LIS input file containing LANDCOVER variable.
:param bool majority: If True, output majority class. If false, output fractions.
"""
if classification_system == "IGBP":
sfctypes = ['Evergreen Needleleaf Forest', 'Evergreen Broadleaf Forest',
'Deciduous Needleleaf Forest', 'Deciduous Broadleaf Forest',
'Mixed Forests', 'Closed Shrublands', 'Open Shrublands',
'Woody Savannas', 'Savannas', 'Grasslands', 'Permanent wetlands',
'Croplands', 'Urban and Built-Up', 'cropland/natural vegetation mosaic',
'Snow and Ice', 'Barren or Sparsely Vegetated', 'Water',
'Wooded Tundra', 'Mixed Tundra', 'Barren Tundra']
else:
raise Exception("This classification system is not yet implemented.")
with Dataset(lis_input_file, mode = "r") as f:
lc = f.variables["LANDCOVER"][:,:,:].data
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
n_types, n_lat, n_lon = lc.shape
if majority:
lc = np.argmax(lc, axis = 0)
lc_string = np.empty(lc.shape, dtype = "<U100")
for i in range(n_lat):
for j in range(n_lon):
lc_string[i,j] = sfctypes[lc[i,j]]
lc = xr.DataArray(
data = lc_string,
dims = ["x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
else:
lc = xr.DataArray(
data = lc,
dims = ["sfctype", "x", "y"],
coords = dict(
sfctype = sfctypes,
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
return lc
def soiltexture(lis_input_file, majority = True, classification_system = "STATSGO"):
"""
Return map with soil texture data from the LIS input file
:param str lis_input_file: Path to LIS input file containing TEXTURE variable.
:param bool majority: If True, output majority class. If false, output fractions.
"""
if classification_system == "STATSGO":
soiltypes = [
'SAND',
'LOAMY SAND',
'SANDY LOAM',
'SILT LOAM',
'SILT',
'LOAM',
'SANDY CLAY LOAM',
'SILTY CLAY LOAM',
'CLAY LOAM',
'SANDY CLAY',
'SILTY CLAY',
'CLAY',
'ORGANIC MATERIAL',
'WATER',
'BEDROCK',
'OTHER(land-ice)',
'PLAYA',
'LAVA',
'WHITE SAND'
]
else:
raise Exception("This classification system is not yet implemented.")
with Dataset(lis_input_file, mode = "r") as f:
tc = f.variables["TEXTURE"][:,:,:].data
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
n_types, n_lat, n_lon = tc.shape
if majority:
tc = np.argmax(tc, axis = 0)
tc_string = np.empty(tc.shape, dtype = "<U100")
for i in range(n_lat):
for j in range(n_lon):
tc_string[i,j] = soiltypes[tc[i,j]]
tc = xr.DataArray(
data = tc_string,
dims = ["x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
else:
tc = xr.DataArray(
data = tc,
dims = ["soiltype", "x", "y"],
coords = dict(
soiltype = soiltypes,
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
return tc
def irrigfrac(lis_input_file):
"""
Return irrigated fraction
:param str lis_input_file: Path to LIS input file containing IRRIGFRAC variable.
"""
with Dataset(lis_input_file, mode = "r") as f:
irrigfrac = f.variables["IRRIGFRAC"][:,:].data
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
irrigfrac = xr.DataArray(
data = np.array(irrigfrac, dtype = '>f4'),
dims = ["x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
return irrigfrac
def topo_complexity(lis_input_file, topo_complexity_file = "/dodrio/scratch/projects/2022_200/project_input/rsda/l_data/obs_satellite/ESA_CCI_SM/ancillary/ESACCI-SOILMOISTURE-TOPOGRAPHIC_COMPLEXITY_V01.1.nc"):
"""
Read in topographic complexity.
:param str lis_input_fille: we will use the latitude and longitude of the domain to crop the topo_complexity file
:param str topo_complexity_file: netcdf file with topographic complexity (code tested for 0.25 degree regular grid)
Note that the resulting topo complexity map will only exactly match the LIS domain
if the latter is on a regular lat-lon grid.
"""
# obtain the LIS domain
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# reading in
with Dataset(topo_complexity_file) as f:
tc = f.variables["topographic_complexity"][:,:].data
lat_map = f.variables["lat"][:].data
lon_map = f.variables["lon"][:].data
tc[tc == -9999] = np.nan
# to xarray
tc = xr.DataArray(
data = tc,
dims = ["lat", "lon"],
coords = dict(
lon = lon_map,
lat = lat_map,
),
)
# sort from low to high values before cropping
tc = tc.sortby("lat", "lon")
# cropping
tc = tc.sel(lat = slice(lats.min(), lats.max()),
lon = slice(lons.min(), lons.max()))
# Now make a new xarray with "lat" and "lon" as coordinates but not dimensions,
# to be consistent with other maps that are not necessarily on a regular grid.
# This ensures tc can be used for masking such maps, for example.
tc = xr.DataArray(
data = tc.data,
dims = ["x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
),
)
return tc
def lis_cube(lis_dir, lis_input_file, var, start, end, subfolder = "SURFACEMODEL",
h = 0, d = "01", freq = "1D", date_shift = False):
"""
Read data cube of LIS model output
:param str lis_dir: parent directory of the LIS output
:param str lis_input_file: path to LIS input file containing the lat/lon information
:param str var: which variable to read in the data cube for (e.g., "SoilMoist_tavg", "LAI_inst", ...)
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param str subfolder: folder where the LIS output is stored (e.g., "SURFACEMODEL", "RTM", ...)
:param int h: UTC time at which LIS outputs (e.g., daily outputs at 12 UTC: h = 12)
:param str d: domain (in filename)
:param str freq: temporal resolution of the output
:param bool date_shift: use this option to shift the LIS output date with "freq". Recommended option for "_tavg" output.
"""
# warnings related to the date_shift option
if ("_tavg" in var) and (not date_shift):
print("It is recommended to set date_shift=True for averaged outputs.")
elif ("_inst" in var) and (date_shift):
print("It is recommended to set date_shift=False for instantaneous outputs.")
# construct a list of all dates
date_list = pd.date_range(start = datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])),
end = datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])), freq = freq)
n_time = len(date_list)
# obtain latitude and longitude from lis input file (the output files have missing values)
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
# read in one output file to obtain the number of layers
date = date_list[0]
fname = "{}/{}/{}{:02d}/LIS_HIST_{}{:02d}{:02d}{:02d}00.d{}.nc".\
format(lis_dir, subfolder, date.year, date.month, date.year, date.month, date.day, date.hour+h, d)
with Dataset(fname, mode = 'r') as f:
output = f.variables[var][:].data
if len(output.shape) == 2:
# e.g., LAI
n_layers = 1
else:
# e.g., soil moisture
n_layers = output.shape[0]
del date, output
# initialize data cube object
if n_layers == 1:
dc = np.ones((n_time, n_lat, n_lon))*np.nan
else:
dc = np.ones((n_time, n_layers, n_lat, n_lon))*np.nan
for i, date in tqdm(enumerate(date_list + pd.Timedelta(freq) if date_shift else date_list), total = n_time):
fname = "{}/{}/{}{:02d}/LIS_HIST_{}{:02d}{:02d}{:02d}00.d{}.nc".\
format(lis_dir, subfolder, date.year, date.month, date.year, date.month, date.day, date.hour+h, d)
try:
with Dataset(fname, mode = 'r') as f:
dc[i] = f.variables[var][:].data
except FileNotFoundError:
# if the file is not there: keep the time slice filled with NaN
print(f"Warning: could not find file {fname}. Filling time slice with NaN.")
dc[dc == -9999] = np.nan # water
if var == "SoilMoist_inst" or var == "SoilMoist_tavg":
dc[dc > 1] = np.nan # glaciers
# store as xarray
if n_layers == 1:
dc = xr.DataArray(
data = dc,
dims = ["time", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
time = date_list,
),
attrs = dict(
description = "LIS model output",
variale = var
),
)
else:
dc = xr.DataArray(
data = dc,
dims = ["time", "layer", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
layer = [i+1 for i in range(n_layers)],
time = date_list,
),
attrs = dict(
description = "LIS model output",
variable = var
),
)
return dc
def wrf_cube(wrf_dir, var, start, end, d = "01", freq = "1D"):
"""
Read data cube of NU-WRF model output
:param str wrf_dir: parent directory of the LIS output
:param str var: which variable to read in the data cube for (e.g., "V10", "T2", "LAI", "SMOIS")
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param str d: domain (in filename)
:param str freq: temporal resolution of the output
"""
# construct a list of all dates
date_list = pd.date_range(
start = datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])),
end = datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])),
freq = freq
)
n_time = len(date_list)
# use the first file to obtain latitude and longitude information, and the number of layers
first_date = date_list[0]
first_file = f"{wrf_dir}/wrfout_d{d}_{first_date.year}-{first_date.month:02}-{first_date.day:02}_{first_date.hour:02}:00:00"
with Dataset(first_file, mode = "r") as f:
# first dimension is dummy time
lats = f.variables["XLAT"][0,:,:].data
lons = f.variables["XLONG"][0,:,:].data
landmask = f.variables["LANDMASK"][0,:,:].data
output = f.variables[var][0,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
# number of layers
if len(output.shape) == 2:
# e.g., LAI
n_layers = 1
else:
# e.g., soil moisture
n_layers = output.shape[0]
del first_date, output
# initialize data cube object
if n_layers == 1:
dc = np.ones((n_time, n_lat, n_lon))*np.nan
else:
dc = np.ones((n_time, n_layers, n_lat, n_lon))*np.nan
# read in all the files
for i, date in tqdm(enumerate(date_list), total = n_time):
fname = f"{wrf_dir}/wrfout_d{d}_{date.year}-{date.month:02}-{date.day:02}_{date.hour:02}:00:00"
with Dataset(fname, mode = 'r') as f:
dc[i] = f.variables[var][0,:].data
dc[dc == -9999] = np.nan
# # mask open water for land variables
# if var in ["SMOIS", "SH2O", "LAI", "VEGFRA"]:
# dc[landmask == 0] = np.nan !!!!! has to be done for each time and layer slice
# mask open water for land variables
if var in ["SMOIS", "SH2O"]:
dc[dc >= 1] = np.nan
elif var in ["LAI", "VEGFRA"]:
dc[dc == 0] = np.nan
# store as xarray
if n_layers == 1:
dc = xr.DataArray(
data = dc,
dims = ["time", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
time = date_list,
),
attrs = dict(
description = "NU-WRF model output",
variale = var
),
)
else:
dc = xr.DataArray(
data = dc,
dims = ["time", "layer", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
layer = [i+1 for i in range(n_layers)],
time = date_list,
),
attrs = dict(
description = "NU-WRF model output",
variable = var
),
)
return dc
def innov_cube(lis_dir, lis_input_file, start, end, var = "innov",
subfolder = "EnKF", a = "01", d = "01", freq = None):
"""
Read data cube of LIS model innovations
:param str lis_dir: parent directory of the LIS output
:param str lis_input_file: path to LIS input file containing the lat/lon information
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param str var: which variable to read in the data cube for
:param str subfolder: subfolder in which innovations are stored
:param str a: updated state (in filename)
:param str d: domain (in filename)
:param str freq: desired temporal resolution after resampling
"""
# start and end dates
start_day, start_month, start_year = int(start[0:2]), int(start[3:5]), int(start[6:10])
end_day, end_month, end_year = int(end[0:2]), int(end[3:5]), int(end[6:10])
start_date, end_date = datetime(start_year, start_month, start_day), datetime(end_year, end_month, end_day)
# obtain latitude and longitude from lis input file (the output files have missing values)
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
# count the number of innov files through bash command
n_innovfiles = int(subprocess.run(f"cd {lis_dir}/{subfolder} && find */*_innov.a{a}.d{d}.nc -type f | wc -l",
capture_output = True, shell = True).stdout)
innov_cube = np.ones((n_innovfiles, n_lat, n_lon))*np.nan
innov_dates = np.ones(n_innovfiles, dtype = 'datetime64[ns]')
# name of the variable to read out
var = f"{var}_{a}"
# loop over all innovation files
print("Constructing innovation cube ...")
i = 0
with tqdm(total = n_innovfiles) as pbar:
for subdir, dirs, files in os.walk(f"{lis_dir}/{subfolder}"):
for filename in files:
if f"innov.a{a}.d{d}" in filename:
year = int(filename[12:16])
month = int(filename[16:18])
day = int(filename[18:20])
hour = int(filename[20:22])
minute = int(filename[22:24])
try:
innov_dates[i] = datetime(year, month, day, hour, minute)
# if (start_date <= innov_dates[i]) & (innov_dates[i] <= end_date):
with Dataset(f"{subdir}/{filename}", mode = "r") as f:
innov_cube[i] = f.variables[var][:].data
i += 1
pbar.update(1)
except IndexError:
# folder has changed: ignore
print("Is the experiment still running? Detected recently added files; these were not read in!")
innov_cube[innov_cube == -9999] = np.nan # water
# store as xarray
dc = xr.DataArray(
data = innov_cube,
dims = ["time", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
time = innov_dates,
),
attrs = dict(
description = "LIS innovations",
variable = var,
),
)
# resample to desired frequency
dc = dc.sortby("time") # necessary step before resampling
if freq is not None:
dc = dc.resample(time = freq).mean()
# only keep observations within the desired time frame
if (start is not None) and (end is not None):
dc = dc[(datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])) <= pd.to_datetime(dc.time)) &
(pd.to_datetime(dc.time) <= datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])))]
return dc
def incr_cube(lis_dir, lis_input_file, start, end,
var = "Soil Moisture", layers = [1,2,3,4],
subfolder = "EnKF", a = "01", d = "01", freq = None):
"""
Read data cube of LIS model increments
:param str lis_dir: parent directory of the LIS output
:param str lis_input_file: path to LIS input file containing the lat/lon information
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param str var: the variable name in the increments file
(e.g., "Soil Moisture", "LAI", ...)
:param list layers: which layers to read in the increments for (choose None for LAI)
:param str subfolder: subfolder in which increments are stored
:param str a: updated state (in filename)
:param str d: domain (in filename)
:param str freq: desired temporal resolution after resampling
"""
# obtain latitude and longitude from lis input file (the output files have missing values)
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
# number of layers to read in
n_layers = 1 if layers is None else len(layers)
# count the number of increment files through bash command
n_incrfiles = int(subprocess.run(f"cd {lis_dir}/{subfolder} && find */*_incr.a{a}.d{d}.nc -type f | wc -l",
capture_output = True, shell = True).stdout)
incr_cube = np.ones((n_incrfiles, n_layers, n_lat, n_lon))*np.nan
incr_dates = np.ones(n_incrfiles, dtype = 'datetime64[ns]')
# loop over all increment files
print("Constructing increment cube ...")
i = 0
with tqdm(total = n_incrfiles) as pbar:
for subdir, dirs, files in os.walk(f"{lis_dir}/{subfolder}"):
for filename in files:
if f"incr.a{a}.d{d}" in filename:
year = int(filename[12:16])
month = int(filename[16:18])
day = int(filename[18:20])
hour = int(filename[20:22])
minute = int(filename[22:24])
try:
incr_dates[i] = datetime(year, month, day, hour, minute)
with Dataset(f"{subdir}/{filename}", mode = "r") as f:
if layers is None:
var = f"anlys_incr_{var}_{a}"
incr_cube[i, 0, :, :] = f.variables[var][:].data
else:
for layer_idx, layer in enumerate(layers):
var = f"anlys_incr_{var} Layer {layer}_{a}"
incr_cube[i, layer_idx, :, :] = f.variables[var][:].data
i += 1
pbar.update(1)
except IndexError:
# folder has changed: ignore
print("Is the experiment still running? Detected recently added files; these were not read in!")
incr_cube[incr_cube == -9999] = np.nan # water
incr_cube[incr_cube == 0] = np.nan # zero increment = no state update
# store as xarray
dc = xr.DataArray(
data = incr_cube,
dims = ["time", "layer", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
layer = [1] if layers is None else layers,
time = incr_dates,
),
attrs = dict(
description = "LIS increments"
),
)
# for variables without layers (e.g., LAI): omit this dimension
if layers is None:
dc = dc.sel(layer = 1)
# resample to desired frequency
dc = dc.sortby("time") # necessary step before resampling
if freq is not None:
dc = dc.resample(time = freq).mean()
# only keep observations within the desired time frame
if (start is not None) and (end is not None):
dc = dc[(datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])) <= pd.to_datetime(dc.time)) &
(pd.to_datetime(dc.time) <= datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])))]
return dc
def spread_cube(lis_dir, lis_input_file, start, end, variable = "Soil Moisture",
layers = [1,2,3,4], subfolder = "EnKF", h = 0, a = "01", d = "01", freq = "1D"):
"""
Read data cube of LIS ensemble spread.
:param str lis_dir: parent directory of the LIS output
:param str lis_input_file: path to LIS input file containing the lat/lon information
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param str var: which variable to read in the data cube for
:param list layers: which layers to read in the spread for (choose None for LAI)
:param str subfolder: subfolder in which the spread is stored
:param int h: UTC time at which LIS outputs (only if freq = "1D")
:param str a: updated state (in filename)
:param str d: domain (in filename)
:param str freq: temporal resolution of the output
"""
# construct a list of all dates
date_list = pd.date_range(start = datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])),
end = datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])), freq = freq)
n_time = len(date_list)
# obtain latitude and longitude from lis input file (the output files have missing values)
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
# number of layers to read in
n_layers = 1 if layers is None else len(layers)
# initialize data cube object
dc = np.ones((n_time, n_layers, n_lat, n_lon))*np.nan
for i, date in tqdm(enumerate(date_list), total = n_time):
fname = "{}/{}/{}{:02d}/LIS_DA_EnKF_{}{:02d}{:02d}{:02d}00_spread.a{}.d{}.nc".\
format(lis_dir, subfolder, date.year, date.month, date.year, date.month, date.day, date.hour+h, a, d)
#try:
with Dataset(fname, mode = 'r') as f:
if layers is None:
var = f"ensspread_{var}_{a}"
dc[i, 0, :, :] = f.variables[var][:].data
else:
for layer_idx, layer in enumerate(layers):
var = f"ensspread_{var} Layer {layer}_{a}"
dc[i, layer_idx, :, :] = f.variables[var][:].data
#except:
# if the file is not there: keep the time slice filled with NaN
# print(f"Warning: could not find file {fname}. Filling time slice with NaN.")
dc[dc == -9999] = np.nan # water
# store as xarray
dc = xr.DataArray(
data = dc,
dims = ["time", "layer", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
layer = [1] if layers is None else layers,
time = date_list,
),
attrs = dict(
description = "LIS model spread",
variable = var,
),
)
# for variables without layers (e.g., LAI): omit this dimension
if layers is None:
dc = dc.sel(layer = 1)
return dc
def obs_cube(lis_dir, lis_input_file, start, end, rescaled = False,
subfolder = "DAOBS", a = "01", d = "01", freq = None):
"""
Read data cube of binary observations.
:param str lis_dir: parent directory of the LIS output
:param str lis_input_file: path to LIS input file containing the lat/lon information
:param str start: start of the data cube (format "DD/MM/YYYY")
:param str end: end of the data cube (format "DD/MM/YYYY")
:param bool rescaled: whether to read out the rescaled (True) or raw (False) observations
:param str subfolder: subfolder in which the spread is stored
:param str a: updated state (in filename)
:param str d: domain (in filename)
:param str freq: desired temporal resolution after resampling
"""
# obtain latitude and longitude from lis input file (the output files have missing values)
with Dataset(lis_input_file, mode = "r") as f:
lats = f.variables["lat"][:,:].data
lons = f.variables["lon"][:,:].data
# number of grid cells in each direction
n_lat = len(lats[:,0])
n_lon = len(lons[0,:])
n_grid = n_lat*n_lon
# count the number of binary observation files through bash command
print("Counting the number of observations ...")
n_obsfiles = int(subprocess.run(f"cd {lis_dir}/{subfolder} && find */LISDAOBS_*a{a}.d{d}.1gs4r -type f | wc -l",
capture_output = True, shell = True).stdout)
obs_cube = np.ones((n_obsfiles, n_lat, n_lon))*np.nan
obs_dates = np.ones(n_obsfiles, dtype = 'datetime64[ns]')
# loop over all observation files
print("Constructing observation cube ...")
i = 0
with tqdm(total = n_obsfiles) as pbar:
for subdir, dirs, files in os.walk(f"{lis_dir}/{subfolder}"):
for filename in files:
file_a = filename[23:25]
file_d = filename[27:29]
if (file_a == a) and (file_d == d):
year = int(filename[9:13])
month = int(filename[13:15])
day = int(filename[15:17])
hour = int(filename[17:19])
minute = int(filename[19:21])
obs_dates[i] = datetime(year, month, day, hour, minute)
with open(f"{subdir}/{filename}") as fid:
obs = np.fromfile(fid, dtype = '>f4')
n_sources = len(obs) // (n_grid + 2)
if n_sources == 1:
if rescaled and i == 0:
print("Warning: no rescaled observations. Returning original observations")
obs = obs[1:-1] # first and last line are meta-information
elif n_sources == 2:
if not rescaled:
obs = obs[:(len(obs)//2)][1:-1]
else:
obs = obs[(len(obs)//2):][1:-1]
elif i == 0:
print("More than 2 sources. Not supported. A NaN-array will be returned.")
obs_cube[i] = np.reshape(obs, (n_lat, n_lon))
i += 1
pbar.update(1)
obs_cube[obs_cube == -9999] = np.nan # water
# store as xarray
obs_cube = xr.DataArray(
data = obs_cube,
dims = ["time", "x", "y"],
coords = dict(
lon = (["x", "y"], lons),
lat = (["x", "y"], lats),
time = obs_dates,
),
attrs = dict(
description = "Observations obtained form binaray DAOBS files"
),
)
# resample to desired frequency
obs_cube = obs_cube.sortby("time") # necessary step before resampling
if freq is not None:
obs_cube = obs_cube.resample(time = freq).mean()
# only keep observations within the desired time frame
if (start is not None) and (end is not None):
obs_cube = obs_cube[(datetime(int(start[6:10]), int(start[3:5]), int(start[0:2])) <= pd.to_datetime(obs_cube.time)) &
(pd.to_datetime(obs_cube.time) <= datetime(int(end[6:10]), int(end[3:5]), int(end[0:2])))]
return obs_cube