-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathameriflux_base_to_netcdf.py
1258 lines (1173 loc) · 47 KB
/
ameriflux_base_to_netcdf.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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# ~*~ coding: utf8 ~*~
"""Read in and plot the AmeriFlux data.
"""
from __future__ import division, print_function
import argparse
import calendar
import datetime
import glob
import itertools
import logging
import operator
import os.path
import re
import warnings
import zipfile
from typing import List
import hesseflux
import numpy as np
import pandas as pd
import pint_xarray # noqa: F401 # pylint: disable=unused-import
import pvlib.location
import pytz
import xarray
from pytz import UTC
_LOGGER = logging.getLogger(__name__)
MINUTES_PER_HOUR = 60
SECONDS_PER_HOUR = 3600
HOURS_PER_DAY = 24
SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY
MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY
MONTHS_PER_YEAR = 12
OBJECT_DTYPE = np.dtype("O")
MONTH_NAMES = calendar.month_name
HAVE_ZIP_NOT_NETCDF = False
"""Whether to read the data from ZIP or skip to netCDF
True: have ZIP files but not netCDF
False: have netCDF
"""
HALF_HOUR_FREQ_STR = "30T"
HOUR_FREQ_STR = "H"
PARSER = argparse.ArgumentParser(
description=__doc__,
)
PARSER.add_argument(
"ameriflux_root",
help="Directory containing site directories with data.",
)
PARSER.add_argument(
metavar="flux_unit_file",
type=pd.read_csv,
dest="flux_unit_df",
)
PARSER.add_argument(
"measurement_height_file",
type=str,
)
CENTRAL_TIME = pytz.timezone("US/Central")
# README_AmeriFlux_BASE.txt says missing value is -9999
MISSING_VALUES = [
"-{nines:s}{dot:s}".format(nines="9" * i, dot=dot)
for i in range(4, 5)
for dot in (".", "")
]
HALF_HOUR = datetime.timedelta(minutes=30)
HOUR = datetime.timedelta(hours=1)
N_BOOTSTRAP_SAMPLES = 5
"""Number of bootstrap samples to use for ustar filtering.
1 is fastest, five seems reasonable, 20 is slow.
"""
CASA_START = "2003-01-01T00:00+00:00"
CASA_END = "2019-12-31T23:59:59.999999999+00:00"
NOW_TIME = datetime.datetime.now(UTC).replace(microsecond=0, second=0, minute=0)
NOW = NOW_TIME.isoformat()
AMERIFLUX_METADATA_TO_INCLUDE = [
"IGBP",
"LOCATION_LAT",
"LOCATION_LONG",
"STATE",
"URL_AMERIFLUX",
"UTC_OFFSET",
# Reference information not always available
"ACKNOWLEDGEMENT",
"ACKNOWLEDGEMENT_COMMENT",
"CLIMATE_KOEPPEN",
"COUNTRY",
"DOI",
"DOI_CITATION",
"FLUX_MEASUREMENTS_METHOD",
"SITE_NAME",
"IGBP_COMMENT",
"LOCATION_ELEV",
"TERRAIN",
"ASPECT",
"SITE_DESC",
"SITE_FUNDING",
"UTC_OFFSET_COMMENT",
]
MEASUREMENT_METADATA_TO_INCLUDE = [
# Data Quality Flags
"FC_SSITC_TEST",
"NEE_SSITC_TEST",
"NEE_PI_SSITC_TEST",
]
AMF_BASE_VAR_NAME_REGEX = re.compile(
r"^(?P<physical_name>\w+?)(?P<quality_flag1>_SSITC_TEST)?(?:_PI)?(?:_QC)?"
r"(?:_F)?(?:_IU)?(?P<loc_rep_agg>_\d+_\d+_(?:\d+|A)|_\d+)?(?:_(?:SD|N))?$"
)
# def parse_file(ameriflux_file):
# """Pull NEE-related data from AmeriFlux file into DataFrame.
# Parameters
# ----------
# ameriflux_file : str
# Returns
# -------
# pd.DataFrame
# """
# site_name = os.path.basename(os.path.dirname(ameriflux_file))
# site_id = os.path.basename(ameriflux_file)[:5]
# if "-" not in site_id:
# site_id = "{country:2s}-{site:3s}".format(
# country=site_id[:2], site=site_id[2:]
# )
# year_match = re.search(r"\d{4}_", ameriflux_file)
# year = ameriflux_file[year_match.start():year_match.end() - 1]
# year_start = np.datetime64("{year:s}-01-01T00:00-06:00".format(year=year))
# ds = pd.read_csv(
# ameriflux_file, index_col=["time"],
# parse_dates=dict(time=["DoY"]),
# date_parser=lambda doy: (
# year_start + np.array(
# np.round(float(doy) * MINUTES_PER_DAY),
# dtype="m8[m]"
# )
# ),
# na_values=[
# "-{nines:s}{dot:s}".format(nines="9" * n_nines, dot=dot)
# for n_nines in (3, 4, 5, 6)
# for dot in (".", "")
# ]
# )
# nee_ds = ds[[col for col in ds.columns if "NEE" in col]]
# nee_ds.columns = pd.MultiIndex.from_product([[site_id], nee_ds.columns])
# return nee_ds
def variable_sort_key(variable_name):
# type: (str) -> int
"""Return a sort key for the variable name.
Puts the NEE/storage-flux-adjusted variables before the
FC/non-storage-flux-adjusted variables, then puts the site-level
variables (without the location suffix _1_1_1 or similar) before
the individual measurements, then sorts the individual
measurements using the location suffix as an integer (so _1_1_1
becomes 111).
Parameters
----------
variable_name : str
Returns
-------
sort_key : int
"""
result = variable_name.startswith("NEE") * 10000
if variable_name.count("_") > 3:
result += int("".join(variable_name.rsplit("_", 4)[-3:]))
return result
def parse_ameriflux(file_name, unit_df, tower_height_da):
# type: (str, pd.DataFrame, xarray.DataArray) -> xarray.Dataset
"""Parse AmeriFlux data from file_name.
Parameters
----------
file_name : str
unit_df : pd.DataFrame
tower_height_da : xarray.DataArray
Returns
-------
flux_data : xarray.Dataset
"""
# dir_name = os.path.dirname(file_name)
base_name = os.path.basename(file_name)
_LOGGER.info(
"Parsing file %s, decomposed as %s", base_name, base_name[:-4].split("_")
)
_network, site_id, _fmt, ver = base_name[:-4].split("_")
with zipfile.ZipFile(file_name) as site_data_dir:
site_data_names = [
info.filename
for info in site_data_dir.filelist
if info.filename.endswith(".csv")
]
site_data_names_sel = [
name for name in site_data_names if name.split("_")[3] == "HH"
]
if not site_data_names_sel:
site_data_names_sel = site_data_names
# Read site metadata
metadata_names = [
info.filename
for info in site_data_dir.filelist
if info.filename.endswith(".xlsx")
]
# metadata_file = "AMF_{site:s}_BIF_LATEST.xlsx".format(site=site_id)
metadata_file = metadata_names[0]
with site_data_dir.open(metadata_file) as meta_file:
metadata_df = (
pd.read_excel(
meta_file
# os.path.join(dir_name, metadata_file)
)
.set_index(["VARIABLE_GROUP", "VARIABLE"])
.sort_index()
)
# Find timezone offset from UTC
utc_offset = float(
metadata_df.loc[("GRP_UTC_OFFSET", "UTC_OFFSET"), "DATAVALUE"].values[0]
)
utc_offset_val = datetime.timedelta(hours=utc_offset)
# Read site data
with site_data_dir.open(site_data_names_sel[0]) as data_file:
site_data = pd.read_csv(
data_file,
skiprows=2,
# parse_dates=["TIMESTAMP_START", "TIMESTAMP_END"],
infer_datetime_format=True,
na_values=MISSING_VALUES,
).sort_index(1)
# Convert times to UTC
for time_name in ("TIMESTAMP_START", "TIMESTAMP_END"):
site_data[time_name] = (
pd.to_datetime(site_data[time_name], format="%Y%m%d%H%M")
- utc_offset_val
)
# site_data.index = pd.IntervalIndex.from_arrays(
# site_data["TIMESTAMP_START"],
# site_data["TIMESTAMP_END"]
# )
first_span = (
site_data["TIMESTAMP_END"].iloc[0] - site_data["TIMESTAMP_START"].iloc[0]
)
assert (
site_data["TIMESTAMP_END"] - site_data["TIMESTAMP_START"] == first_span
).all()
if first_span == HALF_HOUR:
site_data.index = pd.DatetimeIndex(site_data["TIMESTAMP_START"]).to_period(
HALF_HOUR_FREQ_STR
)
elif first_span == HOUR:
site_data.index = pd.DatetimeIndex(site_data["TIMESTAMP_START"]).to_period(
HOUR_FREQ_STR
)
nee_data = site_data[
[
col_name
for col_name in site_data.columns
# Methane flux is interesting, but not what I'm after here
if ("FC" in col_name and "FCH4" not in col_name)
or "NEE" in col_name
or "SW_IN" in col_name
or "USTAR" in col_name
or "TA" in col_name
]
]
# nee_data.columns = pd.MultiIndex.from_product([[site_id], nee_data.columns])
freq = "30min" if first_span == HALF_HOUR else "1H"
nee_data = nee_data.resample(freq).mean().loc[CASA_START:CASA_END, :]
has_data = ~nee_data.isnull().all(axis=1)
# return nee_data.loc[has_data, :]
nee_data = nee_data.loc[has_data, :]
start_date = nee_data.index[0].start_time.replace(
month=1, day=1, hour=0, minute=0, second=0
)
end_date = nee_data.index[-1].end_time.replace(
month=12, day=31, hour=23, minute=59, second=59
)
nee_data = nee_data.reindex(
pd.period_range(start_date, end_date, freq=freq, name="TIMESTAMP_START")
)
nee_ds = xarray.Dataset.from_dataframe(nee_data).expand_dims("site", 0)
##############################
# Set site metadata
nee_ds.coords["site"] = np.array([site_id], dtype="U6")
nee_ds.coords["site"].attrs.update(
dict(
cf_role="timeseries_id",
standard_name="platform_id",
)
)
nee_ds.attrs["feature_type"] = "timeSeries"
for metadata_name in AMERIFLUX_METADATA_TO_INCLUDE:
try:
coord_val = metadata_df.loc[
(slice(None), metadata_name), "DATAVALUE"
].values[0]
except KeyError:
coord_val = ""
if (
metadata_name.endswith("LAT")
or metadata_name.endswith("LONG")
or metadata_name.endswith("OFFSET")
or metadata_name.endswith("ELEV")
):
try:
coord_val = float(coord_val)
except ValueError:
coord_val = -99
else:
assert np.isfinite(coord_val)
nee_ds.coords[metadata_name] = (
("site",),
np.array([coord_val]),
dict(
long_name=metadata_name,
coverage_content_type="referenceInformation",
),
)
nee_ds.coords["data_version"] = (("site",), [ver], {"long_name": "data version"})
# Add a fallback radiation variable
pv_location = pvlib.location.Location(
nee_ds.coords["LOCATION_LAT"].values[0],
nee_ds.coords["LOCATION_LONG"].values[0],
name=nee_ds.coords["site"].values[0],
altitude=(
nee_ds.coords["LOCATION_ELEV"].values[0]
if "LOCATION_ELEV" in nee_ds.coords
else 0.0
),
)
_LOGGER.debug(pv_location)
nee_ds["SW_IN_CLEARSKY"] = (
("site", "TIMESTAMP_START"),
pv_location.get_clearsky(nee_ds.indexes["TIMESTAMP_START"].to_timestamp())[
"ghi"
].values[np.newaxis, :],
# From https://pvlib-python.readthedocs.io/en/stable/reference/generated/
# pvlib.irradiance.dni.html
# GHI = Global Horizontal Irradiance
# DHI = Diffuse Horizontal Irradiance
# DNI = Direct Normal Irradiance
# Looks like DNI > GHI > DHI; DNI is derived from GHI and DHI
# From https://pvlib-python.readthedocs.io/en/stable/gallery/
# irradiance-transposition/plot_ghi_transposition.html
# GHI is close to photovoltaic input for a flat solar array
{
"standard_name": (
"surface_downwelling_shortwave_flux_in_air_assuming_clear_sky"
),
"units": "W/m^2",
"long_name": (
"downwelling shortwave irradiance at the surface assuming clear sky"
" from pvlib-python"
),
"description": (
"downwelling shortwave irradiance near the surface assuming clear sky."
"\nCalculated using PVLib"
),
},
)
del pv_location
# Update dataset CF metadata
for name, var in sorted(
nee_ds.items(),
key=lambda name_var: (
"{pre:s}_{name:s}".format(
pre="z" if ("FC" in name_var[0] or "NEE" in name_var[0]) else "a",
name=name_var[0],
)
),
):
physical_name = re.sub(AMF_BASE_VAR_NAME_REGEX, r"\g<physical_name>", name)
if name != "SW_IN_CLEARSKY":
# SW_IN_CLEARSKY is my variable, not AmeriFlux's
var.attrs["description"] = unit_df.loc[physical_name, "Description"]
var.attrs["units"] = unit_df.loc[physical_name, "Units"]
if name == "FC" or name.startswith("FC_") or name.startswith("NEE"):
var.attrs["standard_name"] = "surface_upward_mole_flux_of_carbon_dioxide"
if "QC" in name or "SSITC_TEST" in name:
var.attrs["standard_name"] += " status_flag"
var.attrs.update(
{
"coverage_content_type": "qualityInformation",
}
)
else:
var.attrs.update(
dict(
coverage_content_type="physicalMeasurement",
cell_methods="site: point TIMESTAMP_START: mean",
long_name="surface_upward_mole_flux_of_carbon_dioxide",
valid_range=[
-90.0,
+90.0,
], # I see -40 to 20 or so, with fuzzy spike around them
)
)
ds_for_flagging = nee_ds[[name]]
for met_var in ["USTAR", "TA", "SW_IN"]:
trial_name = re.sub(
AMF_BASE_VAR_NAME_REGEX,
r"{met_var:s}\<loc_rep_agg>".format(met_var=met_var),
name,
)
if trial_name in nee_ds:
# Try to get at same level
found_name = trial_name
elif met_var in nee_ds:
# Try to get site-level
found_name = met_var
else:
# Get whatever I can find
found_names = [
trial_name
for trial_name in nee_ds.data_vars
if trial_name.startswith(met_var + "_")
]
if found_names:
found_name = found_names[0]
else:
continue
ds_for_flagging[found_name] = nee_ds[found_name]
NEEDED_UNITS = {"TA": "degC", "USTAR": "meter/second"}
if met_var in NEEDED_UNITS:
try:
# TODO: Convert units earlier
ds_for_flagging[found_name] = (
ds_for_flagging[found_name]
.pint.quantify()
.pint.to(NEEDED_UNITS[met_var])
.pint.dequantify()
)
except (TypeError, ValueError):
if ds_for_flagging[found_name].attrs["units"].replace(
" ", ""
) == NEEDED_UNITS[met_var].replace(" ", ""):
# Already converted
ds_for_flagging[found_name].attrs[
"units"
] = NEEDED_UNITS[met_var]
elif "-" in ds_for_flagging[found_name].attrs["units"]:
ds_for_flagging[found_name].attrs["units"] = (
ds_for_flagging[found_name]
.attrs["units"]
.replace("-", "^-")
)
ds_for_flagging[found_name] = (
ds_for_flagging[found_name]
.pint.quantify()
.pint.to(NEEDED_UNITS[met_var])
.pint.dequantify()
)
else:
warnings.warn(
"Have units {:s}, need units {:s}".format(
ds_for_flagging[found_name].attrs["units"],
NEEDED_UNITS[met_var],
),
)
# Include only the time-series coords for time-series flagging
for coord_name in ds_for_flagging.coords:
if "TIMESTAMP_START" not in ds_for_flagging.coords[coord_name].dims:
del ds_for_flagging.coords[coord_name]
_LOGGER.debug(ds_for_flagging)
df_for_flagging = ds_for_flagging.isel(site=0).to_dataframe()
df_for_flagging = df_for_flagging.loc[
:, df_for_flagging.dtypes != object
]
df_for_flagging.index = df_for_flagging.index.to_timestamp()
mask_for_flagging = df_for_flagging.isna()
df_for_flagging = df_for_flagging.fillna(-9999)
_LOGGER.debug(df_for_flagging.dtypes)
_LOGGER.info("Starting MAD spike flagging")
outlier_flags = hesseflux.madspikes(
df_for_flagging,
mask_for_flagging,
)
nee_ds.coords["{name:s}_outlier_flag".format(name=name)] = (
("site", "TIMESTAMP_START"),
outlier_flags[name]
.to_numpy(np.int8, na_value=-99)
.reshape(1, -1),
{
"standard_name": "spike_test_quality_flag",
"flag_values": np.array([0, 2], np.int8),
"flag_meanings": "good outlier",
"valid_range": np.array([0, 2], np.int8),
"comments": "Uses hesseflux implementation",
"references": """
Papale, D., M. Reichstein, M. Aubinet, E. Canfora, C. Bernhofer,
W. Kutsch, B. Longdoz, et al. 2006. “Towards a Standardized Processing
of Net Ecosystem Exchange Measured with Eddy Covariance Technique:
Algorithms and Uncertainty Estimation.” Biogeosciences 3 (4):
571–83. https://doi.org/10.5194/bg-3-571-2006.
""".strip(),
},
)
nee_ds[name].attrs["ancillary_variables"] = (
nee_ds[name].attrs.get("ancillary_variables", "") + " {name:s}_outlier_flag".format(name=name)
).strip()
_LOGGER.info("Starting u-star flagging")
ustar_thresh, ustar_flags = hesseflux.ustarfilter(
df_for_flagging,
mask_for_flagging,
nboot=N_BOOTSTRAP_SAMPLES,
)
_LOGGER.info("Creating u-star flag variables")
nee_ds.coords["{name:s}_ustar_flag".format(name=name)] = (
("site", "TIMESTAMP_START"),
ustar_flags.to_numpy(np.int8, na_value=-99).reshape(1, -1),
{
"standard_name": "multi_variate_test_quality_flag",
"flag_values": [0, 2],
"flag_meanings": "good ustar_below_threshold",
"comments": "Uses hesseflux implementation",
"references": """
Papale, D., M. Reichstein, M. Aubinet, E. Canfora, C. Bernhofer,
W. Kutsch, B. Longdoz, et al. 2006. “Towards a Standardized Processing
of Net Ecosystem Exchange Measured with Eddy Covariance Technique:
Algorithms and Uncertainty Estimation.” Biogeosciences 3 (4):
571–83. https://doi.org/10.5194/bg-3-571-2006.
""".strip(),
},
)
nee_ds[name].attrs["ancillary_variables"] += " {name:s}_ustar_flag".format(name=name)
nee_ds.coords["{name:s}_ustar_thresholds".format(name=name)] = (
# Change "year" to "season" if seasonout=True
("site", "quantile", "year"),
ustar_thresh.reshape(1, 3, -1),
{
"long_name": "ustar_threshold_and_confidence_interval",
"comments": "Uses hesseflux implementation",
"references": """
Papale, D., M. Reichstein, M. Aubinet, E. Canfora, C. Bernhofer,
W. Kutsch, B. Longdoz, et al. 2006. “Towards a Standardized Processing
of Net Ecosystem Exchange Measured with Eddy Covariance Technique:
Algorithms and Uncertainty Estimation.” Biogeosciences 3 (4):
571–83. https://doi.org/10.5194/bg-3-571-2006.
""".strip(),
},
)
nee_ds[name].attrs["ancillary_variables"] += " {name:s}_ustar_thresholds".format(name=name)
nee_ds.coords["quantile"] = (
("quantile",),
np.array([5, 50, 95], np.float32),
{
"long_name": "quantile_values",
"units": "percent",
"n_bootstrap_samples": N_BOOTSTRAP_SAMPLES,
},
)
nee_ds.coords["year"] = (
("year",),
np.arange(
df_for_flagging.index.min().year,
df_for_flagging.index.max().year + 1,
dtype=np.int16,
),
{
"standard_name": "time",
"axis": "T",
"units": "years since 0000-01-01T00:00:00Z",
},
)
if name.startswith("NEE"):
var.attrs.update(
{
"description": (
"Estimate of upward mole flux of carbon dioxide"
" at the surface"
),
}
)
else:
# name.startswith("FC")
var.attrs.update(
{
"description": (
"Eddy-covariance measurement of upward mole "
"flux of carbon dioxide at sensor level"
),
}
)
nee_ds.coords["raw_{:s}".format(name)] = (
var.dims,
var.data
)
# df_for_flagging.index = nee_ds.indexes["TIMESTAMP_START"]
nee_ds[name] = ( # var.copy(False, df_for_flagging[name].values[np.newaxis, :])
# var.dims,
# var[outlier_flags + ustar_flags == 0]
var.where(
(
outlier_flags[name].to_numpy(np.int8, na_value=-99).reshape(1, -1)
+ ustar_flags.to_numpy(np.int8, na_value=-99).reshape(1, -1)
) == 0
)
)
nee_ds[name].attrs["ancillary_variables"] = "{} raw_{:s}".format(
nee_ds[name].attrs.get("ancillary_variables", ""),
name
)
# xarray.DataArray.from_series(df_for_flagging[name])
# if "FCH4" in name:
# var.attrs["units"] = "nmol/m^2/s"
# else:
# var.attrs["standard_name"] = "surface_upward_mole_flux_of_carbon_dioxide"
# var.attrs["units"] = "umol/m^2/s"
qc_flag_names = [name for name in nee_ds if "_QC" in name or "_SSITC_TEST" in name]
nee_ds = nee_ds.set_coords(qc_flag_names)
for name in qc_flag_names:
described_name = re.sub(
AMF_BASE_VAR_NAME_REGEX, r"\g<physical_name>\g<loc_rep_agg>", name
)
described_var = nee_ds[described_name]
var = nee_ds[name]
described_var_ancillaries = described_var.attrs.get(
"ancillary_variables", ""
).split()
if name not in described_var_ancillaries:
described_var_ancillaries.append(name)
described_var.attrs["ancillary_variables"] = " ".join(described_var_ancillaries)
var.attrs["standard_name"] = "quality_flag"
if "_SSITC_TEST" in name:
nee_ds[name] = var = var.astype(np.int8)
nee_ds[described_name] = nee_ds[described_name].where(var < 2)
var.attrs.update(
{
"valid_range": np.array([0, 2], dtype=np.int8),
"flag_values": np.array([0, 1, 2], dtype=np.int8),
"flag_meanings": " ".join(
["data_qc_good", "data_qc_fair", "data_qc_poor"]
),
"description": """
Results of the quality flagging for {described_name:s} according to Foken et al
(2004), based on a combination of Steady State and Integral Turbulence
Characteristics tests by Foken and Wichura (1996) (i.e., 0, 1, 2)
""".format(
described_name=described_name
).strip(),
"references": """
Foken, Th., and B. Wichura. 1996. “Tools for Quality Assessment of
Surface-Based Flux Measurements.” Agricultural and Forest Meteorology
78 (1–2): 83–105. https://doi.org/10.1016/0168-1923(95)02248-1.
Foken, Thomas, Mathias Göockede, Matthias Mauder, Larry Mahrt, Brian
Amiro, and William Munger. 2004. “Post-Field Data Quality Control.” In
Handbook of Micrometeorology: A Guide for Surface Flux Measurement and
Analysis, edited by Xuhui Lee, William Massman, and Beverly Law, 1st
ed., 181–208. Atmospheric and Oceanographic Sciences Library
29. Dordrecht: Springer
Netherlands. https://doi.org/10.1007/1-4020-2265-4_9.
""".strip(),
}
)
for name in nee_ds.data_vars.keys():
if (
site_id in tower_height_da.indexes["Site_ID"]
and name in tower_height_da.indexes["Variable"]
):
tower_height_var = tower_height_da.sel(
Site_ID=site_id,
Variable=name,
Start_Date=var.indexes["TIMESTAMP_START"].to_timestamp(),
).ffill("Start_Date")
nee_ds.coords["height"] = (
("site", "TIMESTAMP_START"),
tower_height_var.data.reshape(1, -1),
{
"standard_name": "height",
"long_name": "sensor_height_above_ground",
"units": "meters",
"positive": "up",
"axis": "Z",
},
)
optimum_name = min(nee_ds.data_vars.keys(), key=variable_sort_key)
nee_ds["ameriflux_single_flux_variable_per_tower"] = (
nee_ds[optimum_name]
)
nee_ds["single_variable_per_tower_name"] = (
("site",),
np.array([optimum_name], dtype="S"),
)
return nee_ds
def save_dataset_netcdf(dataset, filename):
# type: (xarray.Dataset, str) -> None
"""Save the dataset as a netcdf.
Parameters
----------
dataset : xarray.Dataset
Dataset to save
filename : str
Name to save it under
"""
# Avoid propagating changes to caller. I could probably achieve a
# similar effect with dataset.set_coords().
dataset = dataset.copy()
period_index = dataset.indexes["TIMESTAMP_START"]
dataset.coords["time_bnds"] = xarray.DataArray(
np.column_stack([period_index.start_time, period_index.end_time]),
dims=("TIMESTAMP_START", "bnds2"),
attrs=dict(standard_name="time", coverage_content_type="coordinate"),
)
dataset.coords["TIMESTAMP_START"] = xarray.DataArray(
period_index.start_time,
dims="TIMESTAMP_START",
attrs=dict(
standard_name="time",
axis="T",
bounds="time_bnds",
long_name="start_of_observation_period",
coverage_content_type="coordinate",
freq=period_index.freqstr,
),
)
# if resolution == "half_hour":
# dataset.attrs["time_coverage_resolution"] = "P0000-00-00T00:30:00"
# else:
# dataset.attrs["time_coverage_resolution"] = "P0000-00-00T01:00:00"
dataset.attrs["time_coverage_resolution"] = period_index.freq.delta.isoformat()
dataset.attrs["time_coverage_start"] = str(
min(dataset.coords["TIMESTAMP_START"].values)
)
dataset.attrs["time_coverage_end"] = str(
max(dataset.coords["TIMESTAMP_START"].values)
)
dataset.attrs["time_coverage_duration"] = operator.sub(
*dataset.indexes["TIMESTAMP_START"][[-1, 0]]
).isoformat()
dataset.coords["time_written"] = NOW_TIME.time().isoformat()
dataset.coords["date_written"] = NOW_TIME.date().isoformat()
encoding = {name: {"_FillValue": -9999, "zlib": True} for name in dataset.data_vars}
encoding.update({name: {"_FillValue": None} for name in dataset.coords})
# Set units for time variables with bounds
for coord_name in dataset.coords:
if "bounds" not in dataset.coords[coord_name].attrs:
continue
if "M8" in dataset.coords[coord_name].dtype.str:
start_time = dataset.coords[coord_name].values[0]
encoding[coord_name]["units"] = "minutes since {:s}".format(str(start_time))
encoding[dataset.coords[coord_name].attrs["bounds"]]["units"] = encoding[
coord_name
]["units"]
dataset.to_netcdf(
filename,
encoding=encoding,
format="NETCDF4",
mode="w",
)
if __name__ == "__main__":
ARGS = PARSER.parse_args()
logging.basicConfig(level=logging.INFO)
BASE_FILE_NAMES = sorted(
glob.glob(os.path.join(ARGS.ameriflux_root, "AMF_US-*_BASE-BADM_*-*.zip"))
)
SITE_NAMES = [os.path.basename(name).split("_")[1] for name in BASE_FILE_NAMES]
# Find height of measurements
MEASUREMENT_HEIGHT_DF = pd.read_csv(
"BASE_MeasurementHeight_20211109.csv", dtype={"Start_Date": pd.Int64Dtype()}
)
US_NEE_INDEX = MEASUREMENT_HEIGHT_DF["Site_ID"].str.startswith(
"US"
) & np.bitwise_or(
*[
MEASUREMENT_HEIGHT_DF["Variable"].str.startswith(var)
for var in ["FC", "NEE"]
]
)
MEASUREMENT_HEIGHT_DF["Start_Date"] = pd.DatetimeIndex(
[
datetime.datetime.strptime(str(val), "%Y%m%d%H%M"[: len(str(val)) - 2])
if "NA" not in str(val)
else datetime.datetime(1970, 1, 1)
for val in MEASUREMENT_HEIGHT_DF["Start_Date"]
]
)
MEASUREMENT_HEIGHT_DS = xarray.Dataset.from_dataframe(
MEASUREMENT_HEIGHT_DF.loc[US_NEE_INDEX, :]
.groupby(["Site_ID", "Variable", "Start_Date"])
.first()
)
# It would be nice to also get instrument types out of this
MEASUREMENT_HEIGHT_DA = (
MEASUREMENT_HEIGHT_DS["Height"]
.sel(
# Start_Date=slice(CASA_START.split("+")[0], NOW.split("+")[0]),
Variable=sorted(
[
name
for name in MEASUREMENT_HEIGHT_DS.coords["Variable"].values
if name.startswith("FC") or name.startswith("NEE")
]
),
Site_ID=sorted(
[
name
for name in MEASUREMENT_HEIGHT_DS.coords["Site_ID"].values
if name.startswith("US-") and name in SITE_NAMES
]
),
)
.ffill("Start_Date")
.resample(Start_Date="30min")
.ffill()
.sel(
Start_Date=slice(
CASA_START.split("+", maxsplit=1)[0], CASA_END.split("+", maxsplit=1)[0]
)
)
).sortby(["Variable", "Site_ID"])
FLUX_UNIT_DF = (
ARGS.flux_unit_df.set_index("Variable").sort_index().sort_index(axis=1)
)
# Read in data
if HAVE_ZIP_NOT_NETCDF:
# Only have ZIP files
# TOWER_DATA = [
# parse_ameriflux(name, FLUX_UNIT_DF, MEASUREMENT_HEIGHT_DA)
# for name in BASE_FILE_NAMES
# # if os.path.exists(name.rsplit("BASE-BADM")[0] + "BIF_LATEST.xlsx")
# ]
# _LOGGER.info("Done reading in")
# # Save the data I just read in, since it takes six hours to
# # process
# for ds, zip_name in zip(TOWER_DATA, BASE_FILE_NAMES):
# save_dataset_netcdf(ds, zip_name.replace(".zip", ".nc"))
TOWER_DATA = []
for zip_name in BASE_FILE_NAMES:
# if not os.path.exists(name.rsplit("BASE-BADM")[0] + "BIF_LATEST.xlsx"):
# continue
_LOGGER.info("Reading file %s", zip_name)
tower_ds = parse_ameriflux(zip_name, FLUX_UNIT_DF, MEASUREMENT_HEIGHT_DA)
save_dataset_netcdf(tower_ds, zip_name.replace(".zip", ".nc"))
else:
# Have converted ZIP files into netCDF files
TOWER_DATA = []
for zip_name in BASE_FILE_NAMES:
dataset = xarray.open_dataset(
zip_name.replace(".zip", ".nc"), decode_times=False
)
interval = np.diff(
xarray.conventions.decode_cf_variable(
"time_bnds", dataset.coords["time_bnds"]
).values,
axis=1,
).mean()
if interval.astype("m8[m]") <= 30:
ds_freq = "30min"
else:
ds_freq = "1H"
period_index = pd.PeriodIndex(
xarray.conventions.decode_cf_variable(
"TIMESTAMP_START", dataset.coords["TIMESTAMP_START"]
).values,
freq=ds_freq,
)
dataset.coords["TIMESTAMP_START"] = (
("TIMESTAMP_START",),
period_index,
dataset.coords["TIMESTAMP_START"].attrs,
dataset.coords["TIMESTAMP_START"].encoding,
)
del dataset.coords["time_bnds"]
TOWER_DATA.append(dataset)
_LOGGER.info("Done writing out preprocessed data")
AMERIFLUX_DATA_VARS = sorted(
{
name
for ds in TOWER_DATA
for name in ds.data_vars
if "_F" not in name and (name.startswith("FC") or name.startswith("NEE"))
}
)
_LOGGER.info("Found flux variables [\n\t%s\n]", "\n\t".join(AMERIFLUX_DATA_VARS))
HALF_HOUR_DATA = [
df
for df in TOWER_DATA
if df.indexes["TIMESTAMP_START"].freqstr == HALF_HOUR_FREQ_STR
]
HOUR_DATA = [
df
for df in TOWER_DATA
if df.indexes["TIMESTAMP_START"].freqstr == HOUR_FREQ_STR
]
def take_var_from_ds(source_dataset, flux_var, apply_qc=False):
# type: (xarray.Dataset, str) -> xarray.DataArray
"""Take var from the dataset, dropping irrelevant aux vars.
Parameters
----------
source_dataset : xarray.Dataset
The dataset from which to take the variable
flux_var : str
The flux variable to extract
apply_qc : bool
Drop data with bad QC? (>=2)
Returns
-------
xarray.DataArray
"""
result = source_dataset[flux_var]
to_drop = [
name
for name in source_dataset.coords
if re.sub(
AMF_BASE_VAR_NAME_REGEX, r"\g<physical_name>\g<loc_rep_agg>", name
)
not in [flux_var, name]
]
result = result.drop_vars(to_drop)
if apply_qc:
result = result.where("SSITC_TEST < 2")
return result
def harmonize_coords(da_lst):
# type: (List[xarray.DataArray]) -> List[xarray.DataArray]
"""Make sure the datasets have the same coords."""
coord_dims = {name: da.coords[name].dims for da in da_lst for name in da.coords}
ancillary_variables = {
name: dict(zip(da.coords[name].dims, da.coords[name].shape))
for da in da_lst
for name in da.attrs.get("ancillary_variables", "").split()
if name in da.coords
}
result = [da.copy() for da in da_lst if da.size > 0]
for da in result:
for coord_name, coord_dim_list in coord_dims.items():
if coord_name not in da.coords and all(
dim in da.dims for dim in coord_dim_list
):
da.coords[coord_name] = (coord_dim_list, np.full_like(da, np.nan))
for aux_name, aux_dims in ancillary_variables.items():
if aux_name not in da.coords:
da.coords[aux_name] = (aux_dims, np.full_like(da, np.nan))
elif "site" not in da.coords[aux_name].dims: