-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc_utils.py
1068 lines (832 loc) · 34.9 KB
/
qc_utils.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
'''
qc_utils.py contains utility scripts to help with quality control tests
'''
import sys
import os
import configparser
import json
import pandas as pd
import numpy as np
import scipy.special
import pathlib
import itertools
import logging
from scipy.optimize import least_squares
import setup
UNIT_DICT = {"temperature" : "degrees C", \
"dew_point_temperature" : "degrees C", \
"wind_direction" : "degrees", \
"wind_speed" : "meters per second", \
"sea_level_pressure" : "hPa hectopascals", \
"station_level_pressure" : "hPa hectopascals"}
# Letters for flags which should exclude data
# Numbers for information flags, the data are valid, but not necessarily adhering to conventions
QC_TESTS = {"C" : "Climatological",
"D" : "Distribution - Monthly",
"E" : "Clean Up",
"F" : "Frequent Value",
"H" : "High Flag Rate",
"K" : "Repeating Streaks",
"L" : "Logic",
"N" : "Neighbour",
"S" : "Spike",
"T" : "Timestamp",
"U" : "Diurnal",
"V" : "Variance",
"W" : "World Records",
"d" : "Distribution - all",
"h" : "Humidity",
"n" : "Precision",
"o" : "Odd Cluster",
"p" : "Pressure",
"w" : "Winds",
"x" : "Excess streak proportion",
"y" : "Repeated Day streaks",
"1" : "Wind logical - calm, masked direction",
}
MDI = -1.e30
FIRST_YEAR = 1700
# These data are retained and processed by the QC tests. All others are not.
WIND_MEASUREMENT_CODES = ["", "N-Normal", "C-Calm", "V-Variable", "9-Missing"]
#*********************************************
# Process the Configuration File
#*********************************************
CONFIG_FILE = "./configuration.txt"
if not os.path.exists(os.path.join(os.path.dirname(__file__), CONFIG_FILE)):
print(f"Configuration file missing - {os.path.join(os.path.dirname(__file__), CONFIG_FILE)}")
sys.exit()
else:
CONFIG_FILE = os.path.join(os.path.dirname(__file__), CONFIG_FILE)
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
#*********************************************
# Statistics
MEAN = config.getboolean("STATISTICS", "mean")
MEDIAN = config.getboolean("STATISTICS", "median")
if MEAN == MEDIAN:
print("Configuration file STATISTICS entry malformed. One of mean or median only")
sys.exit
# MAD = 0.8 SD
# IQR = 1.3 SD
STDEV = config.getboolean("STATISTICS", "stdev")
IQR = config.getboolean("STATISTICS", "iqr")
MAD = config.getboolean("STATISTICS", "mad")
if sum([STDEV, MAD, IQR]) >= 2:
print("Configuration file STATISTICS entry malformed. One of stdev, iqr, median only")
sys.exit
#*********************************************
# Thresholds
DATA_COUNT_THRESHOLD = config.getint("THRESHOLDS", "min_data_count")
HIGH_FLAGGING = config.getfloat("THRESHOLDS", "high_flag_proportion")
# read in logic check list
LOGICFILE = os.path.join(os.path.dirname(__file__), config.get("FILES", "logic"))
#*********************************************
# Neighbour Checks
MAX_NEIGHBOUR_DISTANCE = config.getint("NEIGHBOURS", "max_distance")
MAX_NEIGHBOUR_VERTICAL_SEP = config.getint("NEIGHBOURS", "max_vertical_separation")
MAX_N_NEIGHBOURS = config.getint("NEIGHBOURS", "max_number")
NEIGHBOUR_FILE = config.get("NEIGHBOURS", "filename")
MIN_NEIGHBOURS = config.getint("NEIGHBOURS", "minimum_number")
#*********************************************
# Set up the Classes
#*********************************************
class Meteorological_Variable(object):
'''
Class for meteorological variable. Initialised with metadata only
'''
def __init__(self, name, mdi, units, dtype):
self.name = name
self.mdi = mdi
self.units = units
self.dtype = dtype
def __str__(self):
return f"variable: {self.name}"
__repr__ = __str__
#*********************************************
class Station(object):
'''
Class for station
'''
def __init__(self, stn_id, lat, lon, elev):
self.id = stn_id
self.lat = lat
self.lon = lon
self.elev = elev
def __str__(self):
return f"station {self.id}, lat {self.lat}, lon {self.lon}, elevation {self.elev}"
__repr__ = __str__
#************************************************************************
# Subroutines
#************************************************************************
def get_station_list(restart_id: str = "", end_id: str = "") -> pd.DataFrame:
"""
Read in station list file(s) and return dataframe
:param str restart_id: which station to start on
:param str end_id: which station to end on
:returns: dataframe of station list
"""
# process the station list
station_list = pd.read_fwf(setup.STATION_LIST, widths=(11, 9, 10, 7, 3, 40, 5),
header=None, names=("id", "latitude", "longitude", "elevation", "state",
"name", "wmo"))
# fill empty entries (default NaN) with blank strings
station_list = station_list.fillna("")
# no longer necessary in November 2019 run, kept just in case
# station_list2 = pd.read_fwf(os.path.join(setup.SUBDAILY_ROOT_DIR, "ghcnh-stations-2add.txt"), widths=(11, 9, 10, 7, 35), header=None)
# station_list = station_list.append(station_list2, ignore_index=True)
station_IDs = station_list.id
# work from the end to save messing up the start indexing
if end_id != "":
endindex, = np.where(station_IDs == end_id)
station_list = station_list.iloc[: endindex[0]+1]
# and do the front
if restart_id != "":
startindex, = np.where(station_IDs == restart_id)
station_list = station_list.iloc[startindex[0]:]
return station_list.reset_index(drop=True) # get_station_list
#************************************************************************
def insert_flags(qc_flags: np.ndarray, flags: np.ndarray) -> np.ndarray:
"""
Update QC flags with the new flags
:param array qc_flags: string array of flags
:param array flags: string array of flags
"""
qc_flags = np.core.defchararray.add(qc_flags.astype(str), flags.astype(str))
return qc_flags # insert_flags
#************************************************************************
def populate_station(station: Station, df: pd.DataFrame, obs_var_list: list, read_flags: bool = False) -> None:
"""
Convert Data Frame into internal station and obs_variable objects
:param Station station: station object to hold information
:param DataFrame df: dataframe of input data
:param list obs_var_list: list of observed variables
:param bool read_flags: read in already pre-existing flags
"""
for variable in obs_var_list:
# make a variable
this_var = Meteorological_Variable(variable, MDI, UNIT_DICT[variable], (float))
# store the data
indata = df[variable].fillna(MDI).to_numpy()
indata = indata.astype(float)
# For wind direction and speed only, account for some measurement flags
# Mask data in the Met_Var object used for the tests, but leave dataframe
# unaffected.
if variable in ["wind_direction", "wind_speed"]:
m_code = df[f"{variable}_Measurement_Code"]
# Build up the mask
for c, code in enumerate(WIND_MEASUREMENT_CODES):
if code == "":
# Empty flags converted to NaNs on reading
code = float("NaN")
if c == 0:
mask = (m_code == code)
else:
mask = (m_code == code) | mask
else:
# Doing string comparison
if c == 0:
# Initialise
mask = (m_code.str.startswith(code))
else:
# Combine using or
# e.g. if code = "N-Normal" or "C-Calm" or "" set True
mask = (m_code.str.startswith(code)) | mask
# invert mask and set to missing
indata[~mask] = MDI
this_var.data = np.ma.masked_where(indata == MDI, indata)
if len(this_var.data.mask.shape) == 0:
# single mask value, replace with arrage of True/False's
if this_var.data.mask:
# True
this_var.data.mask = np.ones(this_var.data.shape)
else:
# False
this_var.data.mask = np.zeros(this_var.data.shape)
this_var.data.fill_value = MDI
if read_flags:
# change all empty values (else NaN) to blank
this_var.flags = df[f"{variable}_QC_flag"].fillna("").to_numpy()
else:
# empty flag array
this_var.flags = np.array(["" for i in range(len(this_var.data))])
# and store
setattr(station, variable, this_var)
return # populate_station
#*********************************************
def calculate_IQR(data: np.ndarray, percentile: float = 0.25) -> float:
''' Calculate the IQR of the data '''
try:
sorted_data = sorted(data.compressed())
except AttributeError:
# if not masked array
sorted_data = sorted(data)
n_data = len(sorted_data)
quartile = int(round(percentile * n_data))
return sorted_data[n_data - quartile] - sorted_data[quartile] # calculate_IQR
#*********************************************
def mean_absolute_deviation(data: np.ndarray, median: bool = False) -> float:
''' Calculate the MAD of the data '''
if median:
mad = np.ma.mean(np.ma.abs(data - np.ma.median(data)))
else:
mad = np.ma.mean(np.ma.abs(data - np.ma.mean(data)))
return mad # mean_absolute_deviation
#*********************************************
def linear(X: np.ndarray, p: np.ndarray) -> np.ndarray:
'''
decay function for line fitting
p[0]=intercept
p[1]=slope
'''
return p[1]*X + p[0] # linear
#*********************************************
def residuals_linear(p: np.ndarray, Y: np.ndarray, X: np.ndarray) -> np.ndarray:
'''
Least squared residuals from linear trend
'''
err = ((Y-linear(X, p))**2.0)
return err # residuals_linear
#*********************************************
def gcv_calculate_binmax(indata: np.ndarray, binmin: float, binwidth: float) -> float:
"""
Determine the appropriate largest bin to use.
:param array indata: input data to bin up
:param float binmin: minimum bin value
:param float binwidth: bin width
:returns: binmax (float)
"""
logger = logging.getLogger(__name__)
MAX_N_BINS = 20000
# so that have sufficient x-bins to fit to
if binwidth < 0.1:
binmax = np.max([2 * max(np.ceil(np.abs(indata))), 1])
else:
binmax = np.max([2 * max(np.ceil(np.abs(indata))), 10])
# if too big, then adjust
if (binmax - binmin)/binwidth > MAX_N_BINS:
# too many bins, will run out of memory
logger.warning(f" Too many bins requested: {binmin} to {binmax} in steps of {binwidth}")
binmax = binmin + (MAX_N_BINS * binwidth)
logger.warning(f" Setting binmax to {binmax}")
return binmax #gcv_calculate_binmax
#*********************************************
def gcv_zeros_in_central_section(histogram: np.ndarray, inner_n: int) -> int:
"""
Helper routine for get_critical_values() ["gcv"] to determine if, for a distribution
with multiple peaks, the central section is sufficiently big. When fitting a x^-1 line
in get_critical_values(), this may not work as intended if many of the bins close to x=0 have
y=0. This routine counts the number of zero-valued bins within the inner_n bins.
:param array histogram: histogram of values to assess
:param int inner_n: how many of the inner bins to assess
:returns: n_zeros how many n_zeros within limit bins of the centre
"""
if len(np.nonzero(histogram == 0)[0]) == 0:
# No zero bins, so central section is the whole histogram
return 0
# Use only the central section (as long as it's 5(10) or more bins)
n_zeros = 0
index = 0
while index < inner_n:
# if not gone beyond the end of the histogram
if index >= len(histogram):
break
# Count outwards until there is a zero-valued bin
if histogram[index] == 0:
n_zeros += 1
index += 1
return n_zeros # gcv_zeros_in_central_section
#*********************************************
def gcv_linear_fit_to_log_histogram(histogram: np.array, bins: np.array) -> np.array:
"""
Take the log10 of the histogram values, and fit a linear x^-1 line
:param array histogram: the histogram values to fit
:param array bins: the histogram bins
:returns: array of fit parameters of (norm, slope)
"""
# and take log10
histogram = np.log10(histogram)
# Working in log-yscale
# a 10^bx, expecting b to be negative
a = histogram[np.argmax(histogram)]
b = 1
p0 = np.array([a, b])
result = least_squares(residuals_linear, p0, args=(histogram, bins), max_nfev=10000, verbose=0, method="lm")
return result.x
#*********************************************
def get_critical_values(indata: np.ndarray, binmin: float = 0, binwidth: float = 1,
plots: bool = False, diagnostics: bool = False,
line_label: str = "", xlabel: str = "", title: str = "") -> float:
"""
Plot histogram on log-y scale and fit 1/x decay curve to set threshold
:param array indata: input data to bin up
:param float binmin: minimum bin value
:param float binwidth: bin width
:param bool plots: do the plots
:param bool diagnostics : do diagnostic outputs
:param str line_label: label for plotted histogram
:param str xlabel: label for x axis
:param str title: plot title
:returns:
float critical value
"""
if len(set(indata)) == 1:
# All data at a single value, so set threshold above this
threshold = max(indata) + binwidth
return threshold
elif len(indata) == 0:
# If no data, return 0+binwidth as the threshold to ensure a positive value
threshold = 0+binwidth
return threshold
# Or there is data to process, let's go
# set up the bins and make a histogram. Use Absolute values
binmax = gcv_calculate_binmax(indata, binmin, binwidth)
bins = np.arange(binmin, binmax, binwidth)
full_hist, full_edges = np.histogram(np.abs(indata), bins=bins)
if len(full_hist) <= 1:
threshold = max(indata) + binwidth
return threshold
# Check if the first 5(10) bins have sufficient data
n_zeros = gcv_zeros_in_central_section(full_hist, 5)
if n_zeros >= 3:
# Note: although cannot have streaks length < 2, this is handled
# via the binmin argument (set to 2 in humidity DPD and streaks)
if len(full_hist) > 5:
n_zeros = gcv_zeros_in_central_section(full_hist, 10)
if n_zeros >= 6:
# Extended central bit is mainly zeros
# can't continue, set threshold to exceed data
threshold = max(indata) + binwidth
return threshold
else:
# Extended central bit is mainly zeros
# can't continue, set threshold to exceed data
threshold = max(indata) + binwidth
return threshold
# Use this central section for fitting
# Avoids risk of secondary populations in the distribution affecting the fit
edges = full_edges[:10]
central_hist = full_hist[:10]
# Remove zeros (turn into infs in log-space)
goods, = np.nonzero(central_hist != 0)
hist = central_hist[goods]
edges = edges[goods]
# Get the curve, and the best fit points
fit = gcv_linear_fit_to_log_histogram(hist, edges)
fit_curve = linear(full_edges, fit)
if fit[1] < 0:
# negative slope as expected
# where does *fit* fall below log10(0.1) = -1, then..
try:
fit_below_point1, = np.argwhere(fit_curve < -1)[0]
# find first empty bin after that
first_zero_bin, = np.argwhere(full_hist[fit_below_point1:] == 0)[0]
threshold = binwidth * (binmin + fit_below_point1 + first_zero_bin)
if isinstance(threshold, np.integer):
# JSON encoder can't cope with np.int64 objects
threshold = int(threshold)
except IndexError:
# Too shallow a decay - use default maximum. Retains all data
# If there were a value much higher, then because a negative
# slope the above snippet should run, rather than this one.
threshold = max(indata) + binwidth
else:
# Positive slope - likely malformed distribution. Retains all data
# The test won't work well given the fit, so just take the data max.
threshold = max(indata) + binwidth
if plots:
plot_log_distribution(full_edges, full_hist, fit_curve, threshold, line_label, xlabel, title)
return threshold # get_critical_values
#*********************************************
def plot_log_distribution(edges: np.ndarray, hist: np.ndarray, fit: np.ndarray, threshold: float, line_label: str, xlabel: str, title: str) -> None:
"""
Plot distribution on a log scale and show the fit
"""
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
_, ax = plt.subplots()
# stretch bars, so can run off below 0
# plot_hist = np.array([np.log10(x) if x != 0 else -1 for x in hist])
# plt.step(edges[1:], plot_hist, color='k', label=line_label, where="pre")
# set values == 0 to be 0.01, so can plot on a log plot
plot_hist = np.array([x if x != 0 else 0.01 for x in hist])
plt.step(edges[:-1], plot_hist, color='k', label=line_label, where="mid")
# convert the fit in log space to actuals
fit = [10**i for i in fit]
plt.plot(edges, fit, 'b-', label="best fit")
plt.xlabel(xlabel)
plt.ylabel("Frequency (logscale))")
# set y-lim to something sensible in actual space
plt.ylim([-1.3, max(plot_hist)+0.5])
plt.ylim([0.01, max(plot_hist)*3])
plt.xlim([0, max(edges)])
plt.axvline(threshold, c='r', label=f"threshold = {threshold}")
plt.legend(loc="upper right")
plt.title(title)
plt.yscale("log")
# sort axes formats
ax.yaxis.set_major_formatter(mticker.FuncFormatter(lambda y, _: '{:g}'.format(y)))
if max(edges) > 2:
ax.xaxis.set_major_locator(mticker.MultipleLocator(2))
else:
ax.xaxis.set_major_locator(mticker.MultipleLocator(0.1))
plt.show()
return # plot_log_distribution
#*********************************************
def average(data: np.ndarray) -> float:
"""
Routine to wrap mean or median functions so can easily switch
"""
if MEAN:
return np.ma.mean(data)
elif MEDIAN:
return np.ma.median(data)
# average
#*********************************************
def spread(data: np.ndarray) -> float:
"""
Routine to wrap st-dev, IQR or MAD functions so can easily switch
"""
if STDEV:
return np.ma.std(data)
elif IQR:
try:
return np.subtract(*np.percentile(data.compressed(), [75, 25]))
except AttributeError:
return np.subtract(*np.percentile(data, [75, 25]))
elif MAD:
if MEDIAN:
return np.ma.median(np.ma.abs(data - np.ma.median(data)))
else:
return np.ma.mean(np.ma.abs(data - np.ma.mean(data)))
# spread
#*********************************************
def winsorize(data: np.ndarray, percent: float) -> np.ndarray:
"""
Replace data greater/less than upper/lower percentile with percentile value
"""
for pct in [percent, 100-percent]:
if pct < 50:
percentile = np.percentile(data.compressed(), pct)
locs = np.ma.where(data < percentile)
else:
percentile = np.percentile(data.compressed(), pct)
locs = np.ma.where(data > percentile)
data[locs] = percentile
return data # winsorize
#************************************************************************
def create_bins(data: np.ndarray, width: float, obs_var_name: str, anomalies: bool = False):
bmin = np.floor(np.ma.min(data))
bmax = np.ceil(np.ma.max(data))
try:
bins = np.arange(bmin - (5*width), bmax + (5*width), width)
return bins # create_bins
except (MemoryError, ValueError):
# wierd values (too small/negative or too high means lots of bins)
# for INM00020460 Jan 2021
import qc_tests.world_records as records
# to ensure no overwriting of the obs variable name attribute
if obs_var_name == "station_level_pressure":
var_name = "sea_level_pressure"
# hence use 500hPa as +/- search
else:
var_name = obs_var_name
if var_name in ["station_level_pressure", "sea_level_pressure"]:
pad = 500
else:
pad = 100
bmin = records.mins[var_name]["row"] - pad
bmax = records.maxes[var_name]["row"] + pad
if anomalies:
# for INI0000VOMM June 2021
# reset range to surround zero
bmin = bmin - np.mean([bmin, bmax])
bmax = bmax - np.mean([bmin, bmax])
bins = np.arange(bmin - (5*width), bmax + (5*width), width)
return bins # create_bins
#*********************************************
def gaussian(X: np.ndarray, p: np.ndarray) -> np.ndarray:
'''
Gaussian function for line fitting
p[0]=norm
p[1]=mean
p[2]=sigma
'''
norm, mu, sig = p
return (norm*(np.exp(-((X-mu)*(X-mu))/(2.0*sig*sig)))) # gaussian
#*********************************************
def skew_gaussian(X: np.ndarray, p: np.ndarray) -> np.ndarray:
'''
Gaussian function for line fitting
p[0]=norm
p[1]=mean
p[2]=sigma
p[3]=skew
'''
norm, mu, sig, skew = p
return (norm*(np.exp(-((X-mu)*(X-mu))/(2.0*sig*sig)))) * \
(1 + scipy.special.erf(skew*(X-mu)/(sig*np.sqrt(2)))) # skew_gaussian
#*********************************************
def residuals_skew_gaussian(p: np.ndarray, Y: np.ndarray, X: np.ndarray) -> np.ndarray:
'''
Least squared residuals from linear trend
'''
err = ((Y-skew_gaussian(X, p))**2.0)
return err # residuals_skew_gaussian
#*********************************************
def invert_gaussian(Y: float, p: float) -> float:
'''
X value of Gaussian at given Y
p[0]=norm
p[1]=mean
p[2]=sigma
'''
norm, mu, sig = p
return mu + (sig*np.sqrt(-2*np.log(Y/norm))) # invert_gaussian
#*********************************************
def residuals_gaussian(p: np.ndarray, Y: np.ndarray, X: np.ndarray) -> np.ndarray:
'''
Least squared residuals from linear trend
'''
err = ((Y-gaussian(X, p))**2.0)
return err # residuals_gaussian
#*********************************************
def fit_gaussian(x: np.ndarray, y: np.ndarray,
norm: float, mu: float = MDI,
sig: float = MDI, skew: float = MDI) -> np.ndarray:
'''
Fit a gaussian to the data provided
Inputs:
x - x-data
y - y-data
norm - norm
Outputs:
fit - array of [norm,mu,sigma,(skew)]
'''
if mu == MDI:
mu = np.ma.mean(x)
if sig == MDI:
sig = np.ma.std(x)
if sig == 0:
# calculation of spread hasn't worked for some reason
sig = 3.*np.unique(np.diff(x))[0]
if np.isnan(skew):
# calculation of skew hasn't worked for some reason (e.g. no variation in values, all==0)
skew = 0
# call the appropriate fitting function and routine
if skew == MDI:
p0 = np.array([norm, mu, sig])
result = least_squares(residuals_gaussian, p0, args=(y, x), max_nfev=10000, verbose=0, method="trf", jac="3-point")
else:
p0 = np.array([norm, mu, sig, skew])
result = least_squares(residuals_skew_gaussian, p0, args=(y, x), max_nfev=10000, verbose=0, method="trf", jac="3-point")
return result.x # fit_gaussian
#************************************************************************
def find_gap(hist: np.ndarray, bins: np.ndarray, threshold: float, gap_size: int, upwards: bool = True) -> float:
'''
Walk the bins of the distribution to find a gap and return where it starts
:param array hist: histogram values
:param array bins: bin values
:param flt threshold: limiting value
:param int gap_size: gap size to record
:param bool upwards: for positive part of x-axis
:returns:
flt: gap_start
'''
# start in the centre
start = np.argmax(hist)
n = 0
gap_length = 0
gap_start = 0
while True:
# if bin is zero - could be a gap
if hist[start + n] == 0:
gap_length += 1
if gap_start == 0:
# plus 1 to get upper bin boundary
if (upwards and bins[start + n + 1] >= threshold):
gap_start = bins[start + n + 1]
elif (not upwards and bins[start + n] <= threshold):
gap_start = bins[start + n]
# bin has value
else:
# gap too short
if gap_length < gap_size:
gap_length = 0
gap_start = 0
# found a gap
elif gap_length >= gap_size and gap_start != 0:
break
# increment counters
if upwards:
n += 1
else:
n -= 1
# escape if gone off the end of the distribution
if (start + n == len(hist) - 1) or (start + n == 0):
gap_start = 0
break
return gap_start # find_gap
#*********************************************
def reporting_accuracy(indata: np.ndarray, winddir: bool = False, plots: bool = False) -> float:
'''
Uses histogram of remainders to look for special values
:param array indata: masked array
:param bool winddir: true if processing wind directions
:param bool plots: make plots (winddir only)
:returns: resolution - reporting accuracy (resolution) of data
'''
good_values = indata.compressed()
resolution = -1
if winddir:
# 360/36/16/8/ compass points ==> 1/10/22.5/45/90 deg resolution
if len(good_values) > 0:
hist, binEdges = np.histogram(good_values, bins=np.arange(0, 362, 1))
# normalise
hist = hist / float(sum(hist))
#
if sum(hist[np.arange(90, 360+90, 90)]) >= 0.6:
resolution = 90
elif sum(hist[np.arange(45, 360+45, 45)]) >= 0.6:
resolution = 45
elif sum(hist[np.round(0.1 + np.arange(22.5, 360+22.5, 22.5)).astype("int")]) >= 0.6:
# added 0.1 because of floating point errors!
resolution = 22
elif sum(hist[np.arange(10, 360+10, 10)]) >= 0.6:
resolution = 10
else:
resolution = 1
print(f"Wind dir resolution = {resolution} degrees")
if plots:
import matplotlib.pyplot as plt
plt.clf()
plt.hist(good_values, bins=np.arange(0, 362, 1))
plt.show()
else:
if len(good_values) > 0:
remainders = np.abs(good_values) - np.floor(np.abs(good_values))
hist, binEdges = np.histogram(remainders, bins=np.arange(-0.05, 1.05, 0.1))
# normalise
hist = hist / float(sum(hist))
if hist[0] >= 0.3:
if hist[5] >= 0.15:
resolution = 0.5
else:
resolution = 1.0
else:
resolution = 0.1
if plots:
import matplotlib.pyplot as plt
plt.clf()
plt.hist(remainders, bins=np.arange(-0.05, 1.05, 0.1), density=True)
plt.show()
return resolution # reporting_accuracy
#*********************************************
def reporting_frequency(intimes: np.ndarray, inobs: np.ndarray) -> float:
'''
Uses histogram of remainders to look for special values
Works on hourly and above or minute data separately
:param array intimes: array of panda datetimes
:param array inobs: masked array
:returns: frequency - reporting frequency of data (minutes)
'''
masked_times = np.ma.masked_array(intimes, mask=inobs.mask)
frequency = -1
if len(masked_times) > 0:
difference_series = np.ma.diff(masked_times)/np.timedelta64(1, "m")
if np.unique(difference_series)[0] >= 60:
# then most likely hourly or beyond
difference_series = difference_series/60.
hist, binEdges = np.histogram(difference_series, bins=np.arange(1, 25, 1), density=True)
# 1,2,3,6 hours
if hist[0] >= 0.5:
frequency = 60
elif hist[1] >= 0.5:
frequency = 120
elif hist[2] >= 0.5:
frequency = 180
elif hist[3] >= 0.5:
frequency = 240
elif hist[5] >= 0.5:
frequency = 360
else:
frequency = 1440
else:
# have to think about minutes
hist, binEdges = np.histogram(difference_series, bins=np.arange(1, 60, 1), density=True)
# 1,5,10 minutes
if hist[0] >= 0.5:
frequency = 1
elif hist[4] >= 0.5:
frequency = 5
elif hist[9] >= 0.5:
frequency = 10
else:
frequency = 60
return frequency # reporting_frequency
#*********************************************
#DEPRECATED - now in a test
def high_flagging(station: Station) -> bool:
"""
Check flags for each observational variable, and return True if any
has too large a proportion flagged
:param Station station: station object
:returns: bool
"""
bad = False
for ov in setup.obs_var_list:
obs_var = getattr(station, ov)
obs_locs, = np.nonzero(obs_var.data.mask == False)
if obs_locs.shape[0] > 10 * DATA_COUNT_THRESHOLD:
# require sufficient observations to make a flagged fraction useful.
flags = obs_var.flags
flagged, = np.nonzero(flags[obs_locs] != "")
if flagged.shape[0] / obs_locs.shape[0] > HIGH_FLAGGING:
bad = True
print(f"{obs_var.name} flagging rate of {100*(flagged.shape[0] / obs_locs.shape[0]):5.1f}%")
break
return bad # high_flagging
#************************************************************************
def find_country_code(lat: float, lon: float) -> str:
"""
Use reverse Geocoder to find closest city to each station, and hence
find the country code.
:param float lat: latitude
:param float lon: longitude
:returns: [str] country_code
"""
import reverse_geocoder as rg
results = rg.search((lat, lon))
country = results[0]['cc']
return country # find_country_code
#************************************************************************
def find_continent(country_code: str) -> str:
"""
Use ISO country list to find continent from country_code.
:param str country_code: ISO standard country code
:returns: [str] continent
"""
# as maybe run from another directory, get the right path
cwd = pathlib.Path(__file__).parent.absolute()
# prepare look up
with open(f'{cwd}/iso_country_codes.json', 'r') as infile:
iso_codes = json.load(infile)
concord = {}
for entry in iso_codes:
concord[entry["Code"]] = entry["continent"]
return concord[country_code]
#************************************************************************
def prepare_data_repeating_streak(data: np.ndarray, diff:int = 0,
plots:bool = False, diagnostics:bool = False) -> tuple[np.array,