-
Notifications
You must be signed in to change notification settings - Fork 1
/
marginal_covariance_analysis_half_hour.py
1250 lines (1144 loc) · 38.8 KB
/
marginal_covariance_analysis_half_hour.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 ~*~
# pylint: disable=invalid-name
"""Find marginal covariances in space and time."""
from __future__ import division, print_function
import datetime
import inspect
import itertools
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pyproj
import scipy.optimize
import xarray
from bottleneck import nansum
from numpy import cos, exp, newaxis, sin, square
from statsmodels.tools.eval_measures import aic, aicc, bic, hqic
from statsmodels.tsa.stattools import acf, acovf
MINUTES_PER_HOUR = 60
HOURS_PER_DAY = 24
MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY
DAYS_PER_DAY = 1
DAYS_PER_YEAR = 365.2425
DAYS_PER_WEEK = 7
GLOBE = ccrs.Globe(semimajor_axis=6370000, semiminor_axis=6370000)
PROJECTION = ccrs.LambertConformal(
standard_parallels=(30, 60),
central_latitude=40,
central_longitude=-96,
globe=GLOBE,
)
GEOD = pyproj.Geod(sphere=True, a=6370000)
PI_OVER_DAY = np.pi / DAYS_PER_DAY
TWO_PI_OVER_DAY = 2 * PI_OVER_DAY
PI_OVER_YEAR = np.pi / DAYS_PER_YEAR
TWO_PI_OVER_YEAR = 2 * PI_OVER_YEAR
FOUR_PI_OVER_YEAR = 4 * PI_OVER_YEAR
MIN_DIFFERENCES_IN_FIT = HOURS_PER_DAY * DAYS_PER_YEAR * 2
amf_ds = xarray.open_dataset(
"/abl/s0/Continent/dfw5129/ameriflux_netcdf/"
"AmeriFlux_single_value_per_tower_half_hour_data.nc4"
)
casa_ds = xarray.open_mfdataset(
(
"/mc1s2/s4/dfw5129/casa_downscaling/"
"200?-??_downscaled_CASA_L2_Ensemble_Mean_Biogenic_NEE_Ameriflux.nc4"
),
combine="by_coords",
)
sites_in_both = sorted(
set(casa_ds.coords["Site_Id"].values) & set(amf_ds.coords["site"].values)
)
times_in_both = sorted(
set(casa_ds.coords["time"].values) & set(amf_ds.coords["TIMESTAMP_START"].values)
)
amf_data = (
amf_ds["ameriflux_carbon_dioxide_flux_estimate"]
.sel(site=sites_in_both, TIMESTAMP_START=times_in_both)
.stack(data_point=("site", "TIMESTAMP_START"))
.dropna("data_point")
)
casa_data = (
casa_ds["NEE"]
.set_index(ameriflux_tower_location="Site_Id")
.sel(
ameriflux_tower_location=amf_data.coords["site"],
time=amf_data.coords["TIMESTAMP_START"],
)
.dropna("data_point")
)
difference = (amf_data - casa_data).dropna("data_point")
print("Have difference data")
coords = np.empty((difference.shape[0], 3), dtype=np.float32)
values = np.array(difference.values, dtype=np.float32)
data_times_int = (
difference.coords["TIMESTAMP_START"].values.astype("M8[m]").astype("i8")
)
data_times_int -= data_times_int[0]
coords[:, 0] = data_times_int.astype(np.float32)
coords[:, 0] /= MINUTES_PER_DAY
# Set to map coordinates in meters
coords[:, 1:] = PROJECTION.transform_points(
PROJECTION.as_geodetic(),
difference.coords["Longitude"].values,
difference.coords["Latitude"].values,
)[:, :2]
# Convert to kilometers
coords[:, 1:] /= 1e3
to_delete = [
coord_name
for coord_name in difference.coords
if (
coord_name not in difference.dims
and "LOCATION" not in coord_name
and "lat" not in coord_name.lower()
and "lon" not in coord_name.lower()
)
]
for coord_name in to_delete:
del difference.coords[coord_name]
hour_data = np.column_stack([coords, values.astype(np.float32)])
# assert amf_data.attrs["units"] == "umol/m2/s"
# assert casa_data.attrs["units"] == "umol/m2/s"
hour_df = pd.DataFrame(
hour_data, columns=["time_days", "x_km", "y_km", "flux_diff_umol_m2_s"]
)
hour_df.to_csv("ameriflux_minus_casa_hour_towers.csv")
difference_df_rect = difference.to_dataframe(
name="ameriflux_minus_casa_hour_towers_umol_m2_s"
)["ameriflux_minus_casa_hour_towers_umol_m2_s"].unstack(0)
difference_df_rect.to_csv(
"ameriflux-minus-casa-half-hour-towers-difference-data-rect.csv"
)
############################################################
# Create and save a netcdf fiel
difference_xarray = difference.to_dataset(
name="ameriflux_minus_casa_carbon_dioxide_flux"
)
difference_rect_xarray = difference_xarray.unstack("data_point")
for name in ("LOCATION_LAT", "LOCATION_LONG", "LOCATION_ELEV", "Longitude", "Latitude"):
difference_rect_xarray.coords[name] = difference_rect_xarray.coords[name].mean(
"TIMESTAMP_START"
)
difference_rect_xarray[
"ameriflux_minus_casa_carbon_dioxide_flux"
] = difference_rect_xarray["ameriflux_minus_casa_carbon_dioxide_flux"].astype(
np.float32
)
difference_rect_xarray["ameriflux_minus_casa_carbon_dioxide_flux"].attrs.update(
dict(
units="umol/m2/s",
long_name="ameriflux_minus_casa_surface_upward_mole_flux_of_carbon_dioxide",
coverage_content_type="modelResult",
)
)
for name in ("LOCATION_LAT", "Latitude"):
difference_rect_xarray.coords[name].attrs.update(
dict(
units="degrees_north",
standard_name="latitude",
long_name="latitude",
valid_range=(0.0, 90.0),
)
)
for name in ("LOCATION_LONG", "Longitude"):
difference_rect_xarray.coords[name].attrs.update(
dict(
units="degrees_east",
standard_name="longitude",
long_name="longitude",
valid_range=(-180.0, 360.0),
)
)
difference_rect_xarray.coords["site_id"] = difference_rect_xarray.coords["site"]
difference_rect_xarray.coords["site_id"].attrs.update(
dict(
cf_role="timeseries_id",
long_name="AmeriFlux_station_id",
)
)
difference_rect_xarray.coords["site"] = np.arange(difference_rect_xarray.dims["site"])
difference_rect_xarray.coords["TIMESTAMP_START"].attrs.update(
dict(
axis="T",
standard_name="time",
long_name="start_of_ameriflux_averaging_window",
bounds="time_bnds",
)
)
for name in (
"IGBP",
"CLIMATE_KOEPPEN",
"SITE_NAME",
"IGBP_COMMENT",
"LOCATION_ELEV",
"TERRAIN",
"SITE_DESC",
"time_bnds",
):
difference_rect_xarray.coords[name] = amf_ds.coords[name]
for name in ("SITE_FUNDING", "ACKNOWLEDGEMENT"):
difference_rect_xarray.coords["AMERIFLUX_" + name] = amf_ds.coords[name]
PSU = "Pennsylvania State University Department of Meteorology and Atmospheric Science"
UTC = datetime.timezone.utc
NOW = datetime.datetime.now(UTC)
NOW_ISO = NOW.isoformat()
difference_rect_xarray.attrs.update(
dict(
history="""created from processed Ameriflux data files and 500m CASA outputs downscaled using ERA5
ameriflux_history={ameriflux_history}
casa_history={casa_history}
""".format(
ameriflux_history=amf_ds.attrs["history"],
casa_history=casa_ds.attrs["history"],
),
institution=PSU,
title="Ameriflux minus CASA carbon dioxide flux differences",
acknowledgement="CASA: ACT-America\nERA5: ECMWF\nAmeriFlux Towers: {ameriflux_sources:s}".format(
ameriflux_sources=""
),
cdm_data_type="Station",
Conventions="CF-1.7,ACDD-1.3",
creator_email="[email protected]",
creator_institution=PSU,
creator_name="Daniel Wesloh",
creator_type="person",
date_metadata_modified=NOW_ISO,
date_modified=NOW_ISO,
date_created=NOW_ISO,
date_written=NOW.date().isoformat(),
time_written=NOW.time().replace(microsecond=0).isoformat(),
geospatial_lat_min=difference_rect_xarray.coords["Latitude"].min().values,
geospatial_lat_max=difference_rect_xarray.coords["Latitude"].max().values,
geospatial_lat_units="degrees_north",
geospatial_lon_min=difference_rect_xarray.coords["Longitude"].min().values,
geospatial_lon_max=difference_rect_xarray.coords["Longitude"].max().values,
geospatial_lon_units="degrees_east",
product_version=1,
program="NASA EVS",
project="Atmospheric Carbon and Transport-America",
source="CASA from Yu et al. (2020), retrieved from ORNL; AmeriFlux data from various contributors",
standard_name_vocabulary="CF Standard Name table v70",
time_coverage_start=difference_rect_xarray.indexes["TIMESTAMP_START"][
0
].isoformat(),
time_coverage_end=difference_rect_xarray.indexes["TIMESTAMP_START"][
-1
].isoformat(),
time_coverage_duration=(
difference_rect_xarray.indexes["TIMESTAMP_START"][-1]
- difference_rect_xarray.indexes["TIMESTAMP_START"][0]
).isoformat(),
# "P0006-08-30T00:00:00",
time_coverage_resolution="PT1H",
ncei_template_version="NCEI_NetCDF_TimeSeries_Orthogonal_Template_v2.0",
featureType="timeSeries",
)
)
encoding = {
name: {"_FillValue": -99, "zlib": True} for name in difference_rect_xarray.data_vars
}
encoding.update(
{name: {"_FillValue": None} for name in difference_rect_xarray.data_vars}
)
difference_rect_xarray.to_netcdf(
"ameriflux_minus_casa_half_hour_tower_data.nc4",
encoding=encoding,
format="NETCDF4_CLASSIC",
)
# Will be in meters
distance_matrix = pd.DataFrame(
index=difference_df_rect.columns,
columns=difference_df_rect.columns,
dtype=np.float64,
)
site_coords = amf_ds.coords["site"].sel(site=difference_df_rect.columns)
for site1, site2 in itertools.product(site_coords, site_coords):
distance_matrix.loc[site1.values[()], site2.values[()]] = GEOD.line_length(
[site1.coords["LOCATION_LONG"], site2.coords["LOCATION_LONG"]],
[site1.coords["LOCATION_LAT"], site2.coords["LOCATION_LAT"]],
)
# Convert distance to kilometers
# Will improve conditioning of later problems
distance_matrix /= 1000
distance_matrix.to_csv("ameriflux-half-hour-towers-distance-matrix-km.csv")
length_opt = scipy.optimize.minimize_scalar(
fun=lambda length, corr, dist: nansum(square(corr - np.exp(-dist / length))),
args=(difference_df_rect.corr().values, distance_matrix.values),
bounds=(1, 1e3),
method="bounded",
)
print("Optimizing length alone:\n", length_opt)
length_with_nugget_opt = scipy.optimize.minimize(
fun=lambda params, corr, dist: nansum(
square(corr - (params[0] * exp(-dist / params[1]) + (1 - params[0])))
),
# Nondimensional, meters
x0=[0.8, 200],
args=(difference_df_rect.corr().values, distance_matrix.values),
)
print(
"Optimizing length with nugget effect:",
"\nWeight on correlated part:",
length_with_nugget_opt.x[0],
"\nCorrelation length:",
length_with_nugget_opt.x[1],
"\nConvergence:",
length_with_nugget_opt.success,
length_with_nugget_opt.message,
"\nInverse Hessian:\n",
length_with_nugget_opt.hess_inv,
)
def count_pairs(col):
"""Count the number of pairs for each lag.
Parameters
----------
col: pd.Series
Returns
-------
n_pairs: np.ndarray
"""
have_data = ~col.isnull()
n_data = len(col)
embedded = np.zeros(2 * n_data - 1)
embedded[:n_data] = have_data
spectrum = np.fft.fft(embedded)
pair_count = np.round(np.fft.ifft(spectrum.conj() * spectrum).real).astype(int)
return pair_count
print("Starting temporal autocorrelation analysis")
acovf_index = pd.timedelta_range(start=0, freq="1H", periods=24 * 365 * 8)
acovf_data = pd.DataFrame(index=acovf_index)
acf_data = pd.DataFrame(index=acovf_index)
pair_counts = pd.DataFrame(index=acovf_index)
for column in difference_df_rect.columns:
col_data = difference_df_rect.loc[:, column].dropna()
if col_data.shape[0] == 0:
continue
col_data = col_data.resample("1H").mean()
acovf_col = acovf(col_data, missing="conservative")
nlags = len(acovf_col)
acovf_data.loc[acovf_index[:nlags], column] = acovf_col
acf_col = acf(col_data, missing="conservative", nlags=nlags, unbiased=True)
acf_data.loc[acovf_index[:nlags], column] = acf_col
pair_counts.loc[acovf_index[:nlags], column] = count_pairs(col_data)[:nlags]
to_fit = acf_data.loc[~acf_data.isna().all(axis=1), :]
time_in_days = to_fit.index.values.astype("m8[h]").astype(np.int64) / 24
acf_data.to_csv("ameriflux-minus-casa-half-hour-towers-autocorrelation-functions.csv")
pair_counts.to_csv("ameriflux-minus-casa-half-hour-towers-pair-counts.csv")
acovf_data.to_csv("ameriflux-minus-casa-half-hour-towers-autocovariance-functions.csv")
# corr_to_fit, time_in_days = np.broadcast_arrays(to_fit.values, time_in_days[:, newaxis])
# not_nan = np.isfinite(corr_to_fit)
# corr_to_fit = corr_to_fit[not_nan]
# time_in_days = time_in_days[not_nan]
single_time_opt = scipy.optimize.minimize_scalar(
lambda length, acorr, times: nansum(
square(acorr - exp(-times / length)[:, newaxis])
),
args=(to_fit.values, time_in_days),
method="bounded",
bounds=(0, 365 * 5),
)
# Returns just over the lower bound if I make the lower bound more
# than one.
print("Parameters for exp(-dt/T):")
print(single_time_opt.x)
two_time_opt = scipy.optimize.minimize(
fun=lambda params, acorr, times: nansum(
square(
acorr
- (
params[0] * exp(-times / (params[1] * DAYS_PER_WEEK))
+ (1 - params[0]) * exp(-times / (params[2] / HOURS_PER_DAY))
)[:, np.newaxis]
)
),
x0=(0.8, 3.0, 3.0),
args=(to_fit.values, time_in_days),
method="L-BFGS-B",
)
# Around a twenty-day correlation period, with most weight on EC error
print("Parameters for a exp(-dt/T) + (1 - a) exp(-dt/Tec): [a, T, Tec]")
print(two_time_opt)
print("Units: unitless, weeks, hours")
# print("EC time in hours:", two_time_opt.x[2] * HOURS_PER_DAY)
cos_opt = scipy.optimize.minimize(
fun=lambda params, acorr, times: nansum(
square(
# [a, b0, b1, b2, c, d, Td, Ta, To, Tec]
acorr
- (
params[0]
* cos(TWO_PI_OVER_DAY * times)
* exp(-times / (params[6] * DAYS_PER_WEEK))
+ (
params[1] / 10
+ params[2] / 10 * cos(TWO_PI_OVER_YEAR * times)
+ params[3] / 10 * cos(2 * TWO_PI_OVER_YEAR * times)
)
* exp(-times / (params[7] * DAYS_PER_YEAR))
+ params[4] * exp(-times / (params[8] * DAYS_PER_WEEK))
+ params[5] * exp(-times / (params[9] / HOURS_PER_DAY))
) # [:, np.newaxis]
)
),
x0=(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 2, 3, 5, 3.0),
args=(to_fit.mean(axis=1).values, time_in_days),
method="L-BFGS-B",
# options=dict(maxiter=100, disp=True),
)
print(
"Parameters for cosine:",
"\nDaily coefficient and timescale (weeks):",
cos_opt.x[[0, 6]],
"\nAnnual coefficients * 10 and timescale (years):",
cos_opt.x[[1, 2, 3, 7]],
# "\nAnnual timescale in years:", cos_opt.x[7] / DAYS_PER_YEAR,
"\nResidual coefficient and timescale (weeks):",
cos_opt.x[[4, 8]],
"\nEddy Covariance coefficient and timescale (hours):",
cos_opt.x[[5, 9]],
# "\nEC timescale in hours:", cos_opt.x[9] * HOURS_PER_DAY,
"\nConverged:",
cos_opt.success,
cos_opt.message,
)
exp_sin2_opt = scipy.optimize.minimize(
fun=lambda params, acorr, times: nansum(
square(
# [a, b, c, d, ld, la, Td, Ta, To, Tec]
acorr
- (
params[0]
* exp(-((sin(TWO_PI_OVER_DAY * times) / params[4]) ** 2))
* exp(-times / (params[6] * DAYS_PER_WEEK))
+ params[1]
* exp(-((sin(TWO_PI_OVER_YEAR * times) / params[5]) ** 2))
* exp(-times / (params[7] * DAYS_PER_YEAR))
+ params[2] * exp(-times / (params[8] * DAYS_PER_WEEK))
+ params[3] * exp(-times / (params[9] / HOURS_PER_DAY))
)[:, np.newaxis]
)
),
x0=(0.2, 0.2, 0.2, 0.2, 10, 10, 2, 3, 5, 3.0),
args=(to_fit.values, time_in_days),
method="L-BFGS-B",
)
print(
"Parameters for exponential sin-squared:",
"\nDaily coefficient, dieoff, and timescale:",
exp_sin2_opt.x[[0, 4, 6]],
"\nAnnual coefficient, dieoff, and timescale:",
exp_sin2_opt.x[[1, 5, 7]],
"\nAnnual timescale in years:",
exp_sin2_opt.x[7] / DAYS_PER_YEAR,
"\nResidual coefficient and timescale:",
exp_sin2_opt.x[[2, 8]],
"\nEddy Covariance coefficient and timescale:",
exp_sin2_opt.x[[3, 9]],
"\nEC timescale in hours:",
exp_sin2_opt.x[9] * HOURS_PER_DAY,
"\nConverged:",
exp_sin2_opt.success,
exp_sin2_opt.message,
)
acf_data.plot(
subplots=True,
sharex=True,
sharey=True,
xlim=(0, 1e9 * 3600 * 24 * 365.2425 * 5),
ylim=(-0.5, 1),
xticks=pd.timedelta_range(start=0, freq="365D", periods=6).to_numpy().astype(float),
)
plt.savefig("ameriflux_minus_casa_half_hour_tower_data_long.pdf")
plt.xlim(0, 1e9 * 3600 * 24 * 60)
plt.xticks(pd.timedelta_range(start=0, freq="7D", periods=8).to_numpy().astype(float))
plt.savefig("ameriflux_minus_casa_half_hour_tower_data_short.pdf")
# Functions for errors correlated over only a few days
def exp_only(tdata, resid_coef, To, Tec):
"""Current practice: Decaying exponential
d_0 dm_0 a_0
"""
Tec /= HOURS_PER_DAY
To *= DAYS_PER_WEEK
exp = np.exp
result = resid_coef * exp(-tdata / To)
result += (1 - resid_coef) * exp(-tdata / Tec)
return result
def exp_cos_daily(tdata, daily_coef, Td, resid_coef, To, ec_coef, Tec):
"""Errors in daily cycle are correlated day-night
d_c dm_0 a_0
"""
Tec /= HOURS_PER_DAY
To *= DAYS_PER_WEEK
Td *= DAYS_PER_WEEK
exp = np.exp
result = daily_coef * np.cos(TWO_PI_OVER_DAY * tdata) * exp(-tdata / Td)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
# Functions where errors in the daily cycle are correlated over a few
# days and errors in the seasonal cycle are correlated over years
def exp_cos_daily_annual(
tdata,
daily_coef,
Td,
ann_coef0,
ann_coef1,
ann_coef2,
Ta,
resid_coef,
To,
ec_coef,
Tec,
):
"""Errors in daily cycle are correlated day-night, Correlated errors in seasonal cycle
d_c dm_0 a_c
"""
Tec /= HOURS_PER_DAY
Td *= DAYS_PER_WEEK
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
exp = np.exp
cos = np.cos
result = daily_coef * cos(TWO_PI_OVER_DAY * tdata) * exp(-tdata / Td)
result += (
ann_coef0
+ ann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ ann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
) * exp(-tdata / Ta)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_cos_daily_expsin2_annual(
tdata, daily_coef, Td, ann_coef, ann_width, Ta, resid_coef, To, ec_coef, Tec
):
"""Errors in daily cycle correlated day-night + Correlated errors in seasonal cycle are only positive
d_c dm_0 a_p
"""
Tec /= HOURS_PER_DAY
Td *= DAYS_PER_WEEK
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = daily_coef * cos(TWO_PI_OVER_DAY * tdata) * exp(-tdata / Td)
result += ann_coef * exp(
-((np.sin(PI_OVER_YEAR * tdata) / ann_width) ** 2) - tdata / Ta
)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
# Functions where the daily cycle is off the same way for years
def exp_cos_daily_times_annual(
tdata, dann_coef0, dann_coef1, dann_coef2, Tad, resid_coef, To, ec_coef, Tec
):
"""Errors in daily cycle correlated day-night, recur same time next year
d_c dm_c a_0
"""
Tec /= HOURS_PER_DAY
Tad *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = (
cos(TWO_PI_OVER_DAY * tdata)
* (
dann_coef0
+ dann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ dann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
)
* exp(-tdata / Tad)
)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_expsin2_daily_times_cos_annual(
tdata,
daily_width,
dann_coef0,
dann_coef1,
dann_coef2,
Tad,
resid_coef,
To,
ec_coef,
Tec,
):
"""Errors in daily cycle not correlated day-night, may be anticorrelated at some lags
d_p dm_c a_0
"""
Tec /= HOURS_PER_DAY
Tad *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = (
exp(-((np.sin(PI_OVER_DAY * tdata) / daily_width) ** 2))
* (
dann_coef0
+ dann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ dann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
)
* exp(-tdata / Tad)
)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_cos_daily_times_expsin2_annual(
tdata, ann_coef, ann_width, Ta, resid_coef, To, ec_coef, Tec
):
"""Errors in daily cycle correlated day-night, are always of same sign day-day
d_c dm_p a_0
"""
Tec /= HOURS_PER_DAY
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
exp = np.exp
result = (
np.cos(TWO_PI_OVER_DAY * tdata)
* ann_coef
* exp(-((np.sin(PI_OVER_YEAR * tdata) / ann_width) ** 2))
)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
# Functions where seasonal cycle is off and daily cycle is off
def exp_cos_daily_times_cos_annual_plus_cos_annual(
tdata,
dann_coef0,
dann_coef1,
dann_coef2,
Tad,
ann_coef0,
ann_coef1,
ann_coef2,
Ta,
resid_coef,
To,
ec_coef,
Tec,
):
"""Daily cycle errors not correlated day-night, may be anticorrelated, seasonal errors correlated
d_c dm_c a_c
"""
Tec /= HOURS_PER_DAY
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = (
cos(TWO_PI_OVER_DAY * tdata)
* (
dann_coef0
+ dann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ dann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
)
* exp(-tdata / Tad)
)
result += (
ann_coef0
+ ann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ ann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
) * exp(-tdata / Ta)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_expsin2_daily_times_cos_annual_plus_cos_annual(
tdata,
daily_width,
dann_coef0,
dann_coef1,
dann_coef2,
Tad,
ann_coef0,
ann_coef1,
ann_coef2,
Ta,
resid_coef,
To,
ec_coef,
Tec,
):
"""Daily cycle errors not correlated day-night, may be anticorrelated, seasonal errors correlated
d_p dm_c a_c
"""
Tec /= HOURS_PER_DAY
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = (
exp(-((np.sin(PI_OVER_DAY * tdata) / daily_width) ** 2))
* (
dann_coef0
+ dann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ dann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
)
* exp(-tdata / Tad)
)
result += (
ann_coef0
+ ann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ ann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
) * exp(-tdata / Ta)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_cos_daily_times_expsin2_annual_plus_cos_annual(
tdata,
dann_coef,
dann_width,
Tad,
ann_coef0,
ann_coef1,
ann_coef2,
Ta,
resid_coef,
To,
ec_coef,
Tec,
):
"""Daily cycle errors not correlated day-night, may be anticorrelated, seasonal errors correlated"""
Tec /= HOURS_PER_DAY
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
cos = np.cos
exp = np.exp
result = (
dann_coef
* cos(TWO_PI_OVER_DAY * tdata)
* exp(-np.sin(PI_OVER_YEAR * tdata / dann_width) ** 2 - tdata / Tad)
)
result += (
ann_coef0
+ ann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ ann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
) * exp(-tdata / Ta)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
def exp_expsin2_daily_times_expsin2_annual_plus_cos_annual(
tdata,
daily_width,
dann_coef,
dann_width,
Tad,
ann_coef0,
ann_coef1,
ann_coef2,
Ta,
resid_coef,
To,
ec_coef,
Tec,
):
"""Daily cycle errors not correlated day-night, may be anticorrelated, seasonal errors correlated
d_c dm_p a_c
"""
Tec /= HOURS_PER_DAY
Ta *= DAYS_PER_YEAR
To *= DAYS_PER_WEEK
sin = np.sin
cos = np.cos
exp = np.exp
result = dann_coef * exp(
-sin(PI_OVER_DAY * tdata / daily_width) ** 2
- sin(PI_OVER_YEAR * tdata / dann_width) ** 2
- tdata / Tad
)
result += (
ann_coef0
+ ann_coef1 * cos(TWO_PI_OVER_YEAR * tdata)
+ ann_coef2 * cos(FOUR_PI_OVER_YEAR * tdata)
) * exp(-tdata / Ta)
result += resid_coef * exp(-tdata / To)
result += ec_coef * exp(-tdata / Tec)
return result
CORR_FUNS = (
exp_only,
exp_cos_daily,
exp_cos_daily_annual,
exp_cos_daily_expsin2_annual,
exp_cos_daily_times_annual,
exp_expsin2_daily_times_cos_annual,
exp_cos_daily_times_expsin2_annual,
exp_cos_daily_times_cos_annual_plus_cos_annual,
exp_cos_daily_times_expsin2_annual_plus_cos_annual,
exp_expsin2_daily_times_cos_annual_plus_cos_annual,
exp_expsin2_daily_times_expsin2_annual_plus_cos_annual,
)
STARTING_PARAMS = dict(
daily_coef=0.4,
daily_width=0.3,
Td=3.0,
ann_coef0=-1e-3,
ann_coef1=+1e-2,
ann_coef2=+1e-2,
dann_coef=1e-3,
dann_width=0.3,
dann_coef0=-1e-3,
dann_coef1=+1e-2,
dann_coef2=+1e-2,
ann_coef=0.04,
ann_width=0.3,
Ta=3.0,
Tad=3.0,
resid_coef=0.2,
To=3.0,
ec_coef=0.4,
Tec=3.0,
)
COEF_DATA = pd.DataFrame(
columns=STARTING_PARAMS.keys(),
index=pd.MultiIndex.from_product(
[acf_data, [fun.__name__ for fun in CORR_FUNS]],
names=["Site", "Correlation function"],
),
)
COEF_VAR_DATA = pd.DataFrame(
columns=STARTING_PARAMS.keys(),
index=pd.MultiIndex.from_product(
[acf_data, [fun.__name__ for fun in CORR_FUNS]],
names=["Site", "Correlation function"],
),
)
FIT_ERROR = pd.DataFrame(
columns=["weighted_error", "MSE", "MAE", "MAR-r"],
index=pd.MultiIndex.from_product(
[acf_data, [fun.__name__ for fun in CORR_FUNS]],
names=["Site", "Correlation function"],
),
)
INF_CRIT = (aic, aicc, bic, hqic)
IC_DATA = pd.DataFrame(
columns=[ic.__name__ for ic in INF_CRIT],
index=pd.MultiIndex.from_product(
[acf_data, [fun.__name__ for fun in CORR_FUNS]],
names=["Site", "Correlation function"],
),
)
SAMPLE_SIZE = 2000
MVN_LOGPDF = scipy.stats.multivariate_normal.logpdf
MIN_LAGS_FOR_FIT = pd.Timedelta(50, "W")
for column in acf_data.iloc[:, :]:
print(column, flush=True)
data_col = difference_df_rect[column]
acf_pair_counts = pair_counts.loc[:, column].dropna()
acf_col = acf_data[column].dropna()
amf_col = amf_ds["ameriflux_carbon_dioxide_flux_estimate"].sel(site=column)
casa_col = (
casa_ds["NEE"]
.set_index(ameriflux_tower_location="Site_Id")
.sel(ameriflux_tower_location=column)
)
# .dropna("time").resample(time="1H").mean()
fig, axes = plt.subplots(5, 1, figsize=(6.5, 8))
for ax in axes[:-1]:
tmp = ax.axhline(0)
casa_line = casa_col.plot(ax=axes[0])
amf_line = amf_col.plot(ax=axes[0])
tmp = axes[0].set_title("Hourly fluxes")
tmp = axes[0].set_ylabel("Flux\n\N{MICRO SIGN}mol/m\N{SUPERSCRIPT TWO}/s")
tmp = axes[0].set_xlabel("Date")
resampled_casa = casa_col.resample(time="1W").mean()
casa_line = axes[1].plot(
resampled_casa.coords["time"].values, resampled_casa.values, label="CASA"
)
resampled_amf = amf_col.resample(TIMESTAMP_START="1W").mean()
amf_line = axes[1].plot(
resampled_amf.coords["TIMESTAMP_START"].values,
resampled_amf.values,
label="AmeriFlux",
)
tmp = axes[1].set_title("Weekly-average fluxes")
tmp = axes[1].set_ylabel("Flux\n\N{MICRO SIGN}mol/m\N{SUPERSCRIPT TWO}/s")
tmp = fig.legend(
handles=[casa_line[0], amf_line[0]], labels=["CASA", "AmeriFlux"], ncol=2
)
tmp = axes[1].set_xlabel("Date")
tmp = data_col.plot(ax=axes[2])
tmp = axes[2].set_title("Ameriflux minus CASA Residuals")
tmp = axes[2].set_ylabel("Residuals\n(\N{MICRO SIGN}mol/m\N{SUPERSCRIPT TWO}/s)")
tmp = axes[2].set_xlabel("Date")
# tmp = acf_col.plot(ax=axes[3])
tmp = axes[3].plot(acf_col.index, acf_col.values)
tmp = axes[3].set_ylim(-1, 1)
tmp = axes[3].set_title("Autocorrelation function")
tmp = axes[3].set_ylabel("Correlation")
tmp = (acf_pair_counts / 1000).plot(ax=axes[4])
tmp = axes[4].set_ylim(0, acf_pair_counts[0] / 1000)
tmp = axes[4].set_title("Number of pairs used for ACF")
tmp = axes[4].set_ylabel("Count\n(thousands)")
dates = pd.date_range(data_col.index[0], data_col.index[-1], freq="1AS")
minor_dates = pd.date_range(data_col.index[0], data_col.index[-1], freq="3MS")
for ax in axes[:3]:
tmp = ax.set_xlim(data_col.index[0], data_col.index[-1])
tmp = ax.set_xticks(dates)
tmp = ax.set_xticklabels(
dates.strftime("%Y-%m"),
rotation=10,
verticalalignment="top",
horizontalalignment="right",
)
tmp = ax.set_xticks(minor_dates, minor=True)
xticklocs = pd.timedelta_range(start=0, freq="365D", periods=7)
minor_dts = pd.timedelta_range(start=0, freq="91D", periods=28)
for ax in axes[3:]:
tmp = ax.set_xlim(
acf_col.index[0].to_numpy().astype("i8").astype(float),
acf_col.index[-1].to_numpy().astype("i8").astype(float),
)
tmp = ax.set_xticks(xticklocs.to_numpy().astype("i8").astype(float))
tmp = ax.set_xticklabels(xticklocs.days)
tmp = ax.set_xlabel("Lag (days)")
tmp = fig.suptitle(
"{site:s} - {climate:s} - {veg:s}".format(
site=column,
climate=amf_ds.coords["CLIMATE_KOEPPEN"].sel(site=column).values,
veg=amf_ds.coords["IGBP"].sel(site=column).values,
)
)
fig.tight_layout()
fig.subplots_adjust(hspace=1, top=0.93)
fig.savefig(
"{site:s}-ameriflux-minus-casa-resid-acf-counts.pdf".format(site=column)
)
plt.close(fig)