-
Notifications
You must be signed in to change notification settings - Fork 38
/
_time.py
1185 lines (979 loc) · 37.4 KB
/
_time.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
"""Time operations on cubes.
Allows for selecting data subsets using certain time bounds;
constructing seasonal and area averages.
"""
import copy
import datetime
import logging
from typing import Union
from warnings import filterwarnings
import dask.array as da
import iris
import iris.coord_categorisation
import iris.cube
import iris.exceptions
import iris.util
import isodate
import numpy as np
from iris.time import PartialDateTime
from esmvalcore.cmor.check import _get_next_month, _get_time_bounds
from esmvalcore.iris_helpers import date2num
from ._shared import get_iris_analysis_operation, operator_accept_weights
logger = logging.getLogger(__name__)
# Ignore warnings about missing bounds where those are not required
for _coord in (
'clim_season',
'day_of_year',
'day_of_month',
'month_number',
'season_year',
'year',
):
filterwarnings(
'ignore',
"Collapsing a non-contiguous coordinate. "
f"Metadata may not be fully descriptive for '{_coord}'.",
category=UserWarning,
module='iris',
)
def extract_time(cube, start_year, start_month, start_day, end_year, end_month,
end_day):
"""Extract a time range from a cube.
Given a time range passed in as a series of years, months and days, it
returns a time-extracted cube with data only within the specified
time range.
Parameters
----------
cube: iris.cube.Cube
input cube.
start_year: int
start year
start_month: int
start month
start_day: int
start day
end_year: int
end year
end_month: int
end month
end_day: int
end day
Returns
-------
iris.cube.Cube
Sliced cube.
Raises
------
ValueError
if time ranges are outside the cube time limits
"""
t_1 = PartialDateTime(year=int(start_year),
month=int(start_month),
day=int(start_day))
t_2 = PartialDateTime(year=int(end_year),
month=int(end_month),
day=int(end_day))
return _extract_datetime(cube, t_1, t_2)
def _parse_start_date(date):
"""Parse start of the input `timerange` tag given in ISO 8601 format.
Returns a datetime.datetime object.
"""
if date.startswith('P'):
start_date = isodate.parse_duration(date)
else:
try:
start_date = isodate.parse_datetime(date)
except isodate.isoerror.ISO8601Error:
start_date = isodate.parse_date(date)
start_date = datetime.datetime.combine(
start_date, datetime.time.min)
return start_date
def _parse_end_date(date):
"""Parse end of the input `timerange` given in ISO 8601 format.
Returns a datetime.datetime object.
"""
if date.startswith('P'):
end_date = isodate.parse_duration(date)
else:
if len(date) == 4:
end_date = datetime.datetime(int(date) + 1, 1, 1, 0, 0, 0)
elif len(date) == 6:
month, year = _get_next_month(int(date[4:]), int(date[0:4]))
end_date = datetime.datetime(year, month, 1, 0, 0, 0)
else:
try:
end_date = isodate.parse_datetime(date)
except isodate.ISO8601Error:
end_date = isodate.parse_date(date)
end_date = datetime.datetime.combine(end_date,
datetime.time.min)
end_date += datetime.timedelta(seconds=1)
return end_date
def _duration_to_date(duration, reference, sign):
"""Add or subtract a duration period to a reference datetime."""
date = reference + sign * duration
return date
def _select_timeslice(
cube: iris.cube.Cube,
select: np.ndarray,
) -> Union[iris.cube.Cube, None]:
"""Slice a cube along its time axis."""
if select.any():
coord = cube.coord('time')
time_dims = cube.coord_dims(coord)
if time_dims:
time_dim = time_dims[0]
slices = tuple(select if i == time_dim else slice(None)
for i in range(cube.ndim))
cube_slice = cube[slices]
else:
cube_slice = cube
else:
cube_slice = None
return cube_slice
def _extract_datetime(
cube: iris.cube.Cube,
start_datetime: PartialDateTime,
end_datetime: PartialDateTime,
) -> iris.cube.Cube:
"""Extract a time range from a cube.
Given a time range passed in as a datetime.datetime object, it
returns a time-extracted cube with data only within the specified
time range with a resolution up to seconds..
Parameters
----------
cube: iris.cube.Cube
input cube.
start_datetime: PartialDateTime
start datetime
end_datetime: PartialDateTime
end datetime
Returns
-------
iris.cube.Cube
Sliced cube.
Raises
------
ValueError
if time ranges are outside the cube time limits
"""
time_coord = cube.coord('time')
time_units = time_coord.units
if time_units.calendar == '360_day':
if isinstance(start_datetime.day, int) and start_datetime.day > 30:
start_datetime.day = 30
if isinstance(end_datetime.day, int) and end_datetime.day > 30:
end_datetime.day = 30
if not cube.coord_dims(time_coord):
constraint = iris.Constraint(
time=lambda t: start_datetime <= t.point < end_datetime)
cube_slice = cube.extract(constraint)
else:
# Convert all time points to dates at once, this is much faster
# than using a constraint.
dates = time_coord.units.num2date(time_coord.points)
select = (dates >= start_datetime) & (dates < end_datetime)
cube_slice = _select_timeslice(cube, select)
if cube_slice is None:
def dt2str(time: PartialDateTime) -> str:
txt = f"{time.year}-{time.month:02d}-{time.day:02d}"
if any([time.hour, time.minute, time.second]):
txt += f" {time.hour:02d}:{time.minute:02d}:{time.second:02d}"
return txt
raise ValueError(
f"Time slice {dt2str(start_datetime)} "
f"to {dt2str(end_datetime)} is outside "
f"cube time bounds {time_coord.cell(0).point} to "
f"{time_coord.cell(-1).point}.")
return cube_slice
def clip_timerange(cube, timerange):
"""Extract time range with a resolution up to seconds.
Parameters
----------
cube : iris.cube.Cube
Input cube.
timerange : str
Time range in ISO 8601 format.
Returns
-------
iris.cube.Cube
Sliced cube.
Raises
------
ValueError
Time ranges are outside the cube's time limits.
"""
start_date = timerange.split('/')[0]
start_date = _parse_start_date(start_date)
end_date = timerange.split('/')[1]
end_date = _parse_end_date(end_date)
if isinstance(start_date, isodate.duration.Duration):
start_date = _duration_to_date(start_date, end_date, sign=-1)
elif isinstance(start_date, datetime.timedelta):
start_date = _duration_to_date(start_date, end_date, sign=-1)
start_date -= datetime.timedelta(seconds=1)
if isinstance(end_date, isodate.duration.Duration):
end_date = _duration_to_date(end_date, start_date, sign=1)
elif isinstance(end_date, datetime.timedelta):
end_date = _duration_to_date(end_date, start_date, sign=1)
end_date += datetime.timedelta(seconds=1)
t_1 = PartialDateTime(
year=start_date.year,
month=start_date.month,
day=start_date.day,
hour=start_date.hour,
minute=start_date.minute,
second=start_date.second,
)
t_2 = PartialDateTime(
year=end_date.year,
month=end_date.month,
day=end_date.day,
hour=end_date.hour,
minute=end_date.minute,
second=end_date.second,
)
return _extract_datetime(cube, t_1, t_2)
def extract_season(cube, season):
"""Slice cube to get only the data belonging to a specific season.
Parameters
----------
cube: iris.cube.Cube
Original data
season: str
Season to extract. Available: DJF, MAM, JJA, SON
and all sequentially correct combinations: e.g. JJAS
Returns
-------
iris.cube.Cube
data cube for specified season.
Raises
------
ValueError
if requested season is not present in the cube
"""
season = season.upper()
allmonths = 'JFMAMJJASOND' * 2
if season not in allmonths:
raise ValueError(f"Unable to extract Season {season} "
f"combination of months not possible.")
sstart = allmonths.index(season)
res_season = allmonths[sstart + len(season):sstart + 12]
seasons = [season, res_season]
coords_to_remove = []
if not cube.coords('clim_season'):
iris.coord_categorisation.add_season(cube,
'time',
name='clim_season',
seasons=seasons)
coords_to_remove.append('clim_season')
if not cube.coords('season_year'):
iris.coord_categorisation.add_season_year(cube,
'time',
name='season_year',
seasons=seasons)
coords_to_remove.append('season_year')
result = cube.extract(iris.Constraint(clim_season=season))
for coord in coords_to_remove:
cube.remove_coord(coord)
if result is None:
raise ValueError(f'Season {season!r} not present in cube {cube}')
return result
def extract_month(cube, month):
"""Slice cube to get only the data belonging to a specific month.
Parameters
----------
cube: iris.cube.Cube
Original data
month: int
Month to extract as a number from 1 to 12
Returns
-------
iris.cube.Cube
data cube for specified month.
Raises
------
ValueError
if requested month is not present in the cube
"""
if month not in range(1, 13):
raise ValueError('Please provide a month number between 1 and 12.')
if not cube.coords('month_number'):
iris.coord_categorisation.add_month_number(cube,
'time',
name='month_number')
result = cube.extract(iris.Constraint(month_number=month))
if result is None:
raise ValueError(f'Month {month!r} not present in cube {cube}')
return result
def get_time_weights(cube):
"""Compute the weighting of the time axis.
Parameters
----------
cube: iris.cube.Cube
input cube.
Returns
-------
numpy.array
Array of time weights for averaging.
"""
time = cube.coord('time')
coord_dims = cube.coord_dims('time')
# Multidimensional time coordinates are not supported: In this case,
# weights cannot be simply calculated as difference between the bounds
if len(coord_dims) > 1:
raise ValueError(
f"Weighted statistical operations are not supported for "
f"{len(coord_dims):d}D time coordinates, expected "
f"0D or 1D")
# Extract 1D time weights (= lengths of time intervals)
time_weights = time.core_bounds()[:, 1] - time.core_bounds()[:, 0]
return time_weights
def _aggregate_time_fx(result_cube, source_cube):
time_dim = set(source_cube.coord_dims(source_cube.coord('time')))
if source_cube.cell_measures():
for measure in source_cube.cell_measures():
measure_dims = set(source_cube.cell_measure_dims(measure))
if time_dim.intersection(measure_dims):
logger.debug('Averaging time dimension in measure %s.',
measure.var_name)
result_measure = da.mean(measure.core_data(),
axis=tuple(time_dim))
measure = measure.copy(result_measure)
measure_dims = tuple(measure_dims - time_dim)
result_cube.add_cell_measure(measure, measure_dims)
if source_cube.ancillary_variables():
for ancillary_var in source_cube.ancillary_variables():
ancillary_dims = set(
source_cube.ancillary_variable_dims(ancillary_var))
if time_dim.intersection(ancillary_dims):
logger.debug(
'Averaging time dimension in ancillary variable %s.',
ancillary_var.var_name)
result_ancillary_var = da.mean(ancillary_var.core_data(),
axis=tuple(time_dim))
ancillary_var = ancillary_var.copy(result_ancillary_var)
ancillary_dims = tuple(ancillary_dims - time_dim)
result_cube.add_ancillary_variable(ancillary_var,
ancillary_dims)
def hourly_statistics(cube, hours, operator='mean'):
"""Compute hourly statistics.
Chunks time in x hours periods and computes statistics over them.
Parameters
----------
cube: iris.cube.Cube
input cube.
hours: int
Number of hours per period. Must be a divisor of 24
(1, 2, 3, 4, 6, 8, 12)
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min', 'max'
Returns
-------
iris.cube.Cube
Hourly statistics cube
"""
if not cube.coords('hour_group'):
iris.coord_categorisation.add_categorised_coord(
cube,
'hour_group',
'time',
lambda coord, value: coord.units.num2date(value).hour // hours,
units='1')
if not cube.coords('day_of_year'):
iris.coord_categorisation.add_day_of_year(cube, 'time')
if not cube.coords('year'):
iris.coord_categorisation.add_year(cube, 'time')
operator = get_iris_analysis_operation(operator)
result = cube.aggregated_by(['hour_group', 'day_of_year', 'year'],
operator)
result.remove_coord('hour_group')
result.remove_coord('day_of_year')
result.remove_coord('year')
return result
def daily_statistics(cube, operator='mean'):
"""Compute daily statistics.
Chunks time in daily periods and computes statistics over them;
Parameters
----------
cube: iris.cube.Cube
input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'
Returns
-------
iris.cube.Cube
Daily statistics cube
"""
if not cube.coords('day_of_year'):
iris.coord_categorisation.add_day_of_year(cube, 'time')
if not cube.coords('year'):
iris.coord_categorisation.add_year(cube, 'time')
operator = get_iris_analysis_operation(operator)
result = cube.aggregated_by(['day_of_year', 'year'], operator)
result.remove_coord('day_of_year')
result.remove_coord('year')
return result
def monthly_statistics(cube, operator='mean'):
"""Compute monthly statistics.
Chunks time in monthly periods and computes statistics over them;
Parameters
----------
cube: iris.cube.Cube
input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'
Returns
-------
iris.cube.Cube
Monthly statistics cube
"""
if not cube.coords('month_number'):
iris.coord_categorisation.add_month_number(cube, 'time')
if not cube.coords('year'):
iris.coord_categorisation.add_year(cube, 'time')
operator = get_iris_analysis_operation(operator)
result = cube.aggregated_by(['month_number', 'year'], operator)
_aggregate_time_fx(result, cube)
return result
def seasonal_statistics(cube,
operator='mean',
seasons=('DJF', 'MAM', 'JJA', 'SON')):
"""Compute seasonal statistics.
Chunks time seasons and computes statistics over them.
Parameters
----------
cube: iris.cube.Cube
input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'
seasons: list or tuple of str, optional
Seasons to build. Available: ('DJF', 'MAM', 'JJA', SON') (default)
and all sequentially correct combinations holding every month
of a year: e.g. ('JJAS','ONDJFMAM'), or less in case of prior season
extraction.
Returns
-------
iris.cube.Cube
Seasonal statistic cube
"""
seasons = tuple(sea.upper() for sea in seasons)
if any(len(sea) < 2 for sea in seasons):
raise ValueError(
f"Minimum of 2 month is required per Seasons: {seasons}.")
if not cube.coords('clim_season'):
iris.coord_categorisation.add_season(cube,
'time',
name='clim_season',
seasons=seasons)
else:
old_seasons = sorted(set(cube.coord('clim_season').points))
if not all(osea in seasons for osea in old_seasons):
raise ValueError(
f"Seasons {seasons} do not match prior season extraction "
f"{old_seasons}.")
if not cube.coords('season_year'):
iris.coord_categorisation.add_season_year(cube,
'time',
name='season_year',
seasons=seasons)
operator = get_iris_analysis_operation(operator)
result = cube.aggregated_by(['clim_season', 'season_year'], operator)
# CMOR Units are days so we are safe to operate on days
# Ranging on [29, 31] days makes this calendar-independent
# the only season this could not work is 'F' but this raises an
# ValueError
def spans_full_season(cube):
"""Check for all month present in the season.
Parameters
----------
cube: iris.cube.Cube
input cube.
Returns
-------
bool
truth statement if time bounds are within (month*29, month*31)
"""
time = cube.coord('time')
num_days = [(tt.bounds[0, 1] - tt.bounds[0, 0]) for tt in time]
seasons = cube.coord('clim_season').points
tar_days = [(len(sea) * 29, len(sea) * 31) for sea in seasons]
return [dt[0] <= dn <= dt[1] for dn, dt in zip(num_days, tar_days)]
full_seasons = spans_full_season(result)
result = result[full_seasons]
_aggregate_time_fx(result, cube)
return result
def annual_statistics(cube, operator='mean'):
"""Compute annual statistics.
Note that this function does not weight the annual mean if
uneven time periods are present. Ie, all data inside the year
are treated equally.
Parameters
----------
cube: iris.cube.Cube
input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'
Returns
-------
iris.cube.Cube
Annual statistics cube
"""
# TODO: Add weighting in time dimension. See iris issue 3290
# https://github.com/SciTools/iris/issues/3290
operator = get_iris_analysis_operation(operator)
if not cube.coords('year'):
iris.coord_categorisation.add_year(cube, 'time')
result = cube.aggregated_by('year', operator)
_aggregate_time_fx(result, cube)
return result
def decadal_statistics(cube, operator='mean'):
"""Compute decadal statistics.
Note that this function does not weight the decadal mean if
uneven time periods are present. Ie, all data inside the decade
are treated equally.
Parameters
----------
cube: iris.cube.Cube
input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'
Returns
-------
iris.cube.Cube
Decadal statistics cube
"""
# TODO: Add weighting in time dimension. See iris issue 3290
# https://github.com/SciTools/iris/issues/3290
operator = get_iris_analysis_operation(operator)
if not cube.coords('decade'):
def get_decade(coord, value):
"""Categorize time coordinate into decades."""
date = coord.units.num2date(value)
return date.year - date.year % 10
iris.coord_categorisation.add_categorised_coord(
cube, 'decade', 'time', get_decade)
result = cube.aggregated_by('decade', operator)
_aggregate_time_fx(result, cube)
return result
def climate_statistics(cube,
operator='mean',
period='full',
seasons=('DJF', 'MAM', 'JJA', 'SON')):
"""Compute climate statistics with the specified granularity.
Computes statistics for the whole dataset. It is possible to get them for
the full period or with the data grouped by hour, day, month or season.
Parameters
----------
cube: iris.cube.Cube
Input cube.
operator: str, optional
Select operator to apply.
Available operators: 'mean', 'median', 'std_dev', 'sum', 'min',
'max', 'rms'.
period: str, optional
Period to compute the statistic over.
Available periods: 'full', 'season', 'seasonal', 'monthly', 'month',
'mon', 'daily', 'day', 'hourly', 'hour', 'hr'.
seasons: list or tuple of str, optional
Seasons to use if needed. Defaults to ('DJF', 'MAM', 'JJA', 'SON').
Returns
-------
iris.cube.Cube
Climate statistics cube.
"""
original_dtype = cube.dtype
period = period.lower()
if period in ('full', ):
operator_method = get_iris_analysis_operation(operator)
if operator_accept_weights(operator):
time_weights = get_time_weights(cube)
if time_weights.min() == time_weights.max():
# No weighting needed.
clim_cube = cube.collapsed('time', operator_method)
else:
clim_cube = cube.collapsed('time',
operator_method,
weights=time_weights)
else:
clim_cube = cube.collapsed('time', operator_method)
else:
clim_coord = _get_period_coord(cube, period, seasons)
operator = get_iris_analysis_operation(operator)
clim_cube = cube.aggregated_by(clim_coord, operator)
clim_cube.remove_coord('time')
_aggregate_time_fx(clim_cube, cube)
if clim_cube.coord(clim_coord.name()).is_monotonic():
iris.util.promote_aux_coord_to_dim_coord(clim_cube,
clim_coord.name())
else:
clim_cube = iris.cube.CubeList(
clim_cube.slices_over(clim_coord.name())).merge_cube()
cube.remove_coord(clim_coord)
new_dtype = clim_cube.dtype
if original_dtype != new_dtype:
logger.debug(
"climate_statistics changed dtype from "
"%s to %s, changing back", original_dtype, new_dtype)
clim_cube.data = clim_cube.core_data().astype(original_dtype)
return clim_cube
def anomalies(cube,
period,
reference=None,
standardize=False,
seasons=('DJF', 'MAM', 'JJA', 'SON')):
"""Compute anomalies using a mean with the specified granularity.
Computes anomalies based on hourly, daily, monthly, seasonal or yearly
means for the full available period.
Parameters
----------
cube: iris.cube.Cube
Input cube.
period: str
Period to compute the statistic over.
Available periods: 'full', 'season', 'seasonal', 'monthly', 'month',
'mon', 'daily', 'day', 'hourly', 'hour', 'hr'.
reference: list int, optional, default: None
Period of time to use a reference, as needed for the 'extract_time'
preprocessor function. If ``None``, all available data is used as a
reference.
standardize: bool, optional
If ``True`` standardized anomalies are calculated.
seasons: list or tuple of str, optional
Seasons to use if needed. Defaults to ('DJF', 'MAM', 'JJA', 'SON').
Returns
-------
iris.cube.Cube
Anomalies cube.
"""
if reference is None:
reference_cube = cube
else:
reference_cube = extract_time(cube, **reference)
reference = climate_statistics(reference_cube,
period=period,
seasons=seasons)
if period in ['full']:
metadata = copy.deepcopy(cube.metadata)
cube = cube - reference
cube.metadata = metadata
if standardize:
cube_stddev = climate_statistics(cube,
operator='std_dev',
period=period,
seasons=seasons)
cube = cube / cube_stddev
cube.units = '1'
return cube
cube = _compute_anomalies(cube, reference, period, seasons)
# standardize the results if requested
if standardize:
cube_stddev = climate_statistics(cube,
operator='std_dev',
period=period)
tdim = cube.coord_dims('time')[0]
reps = cube.shape[tdim] / cube_stddev.shape[tdim]
if not reps % 1 == 0:
raise ValueError(
"Cannot safely apply preprocessor to this dataset, "
"since the full time period of this dataset is not "
f"a multiple of the period '{period}'")
cube.data = cube.core_data() / da.concatenate(
[cube_stddev.core_data() for _ in range(int(reps))], axis=tdim)
cube.units = '1'
return cube
def _compute_anomalies(cube, reference, period, seasons):
cube_coord = _get_period_coord(cube, period, seasons)
ref_coord = _get_period_coord(reference, period, seasons)
data = cube.core_data()
cube_time = cube.coord('time')
ref = {}
for ref_slice in reference.slices_over(ref_coord):
ref[ref_slice.coord(ref_coord).points[0]] = ref_slice.core_data()
cube_coord_dim = cube.coord_dims(cube_coord)[0]
slicer = [slice(None)] * len(data.shape)
new_data = []
for i in range(cube_time.shape[0]):
slicer[cube_coord_dim] = i
new_data.append(data[tuple(slicer)] - ref[cube_coord.points[i]])
data = da.stack(new_data, axis=cube_coord_dim)
cube = cube.copy(data)
cube.remove_coord(cube_coord)
return cube
def _get_period_coord(cube, period, seasons):
"""Get periods."""
if period in ['hourly', 'hour', 'hr']:
if not cube.coords('hour'):
iris.coord_categorisation.add_hour(cube, 'time')
return cube.coord('hour')
if period in ['daily', 'day']:
if not cube.coords('day_of_year'):
iris.coord_categorisation.add_day_of_year(cube, 'time')
return cube.coord('day_of_year')
if period in ['monthly', 'month', 'mon']:
if not cube.coords('month_number'):
iris.coord_categorisation.add_month_number(cube, 'time')
return cube.coord('month_number')
if period in ['seasonal', 'season']:
if not cube.coords('season_number'):
iris.coord_categorisation.add_season_number(cube,
'time',
seasons=seasons)
return cube.coord('season_number')
raise ValueError(f"Period '{period}' not supported")
def regrid_time(cube, frequency):
"""Align time axis for cubes so they can be subtracted.
Operations on time units, time points and auxiliary
coordinates so that any cube from cubes can be subtracted from any
other cube from cubes. Currently this function supports
yearly (frequency=yr), monthly (frequency=mon),
daily (frequency=day), 6-hourly (frequency=6hr),
3-hourly (frequency=3hr) and hourly (frequency=1hr) data time frequencies.
Parameters
----------
cube: iris.cube.Cube
input cube.
frequency: str
data frequency: mon, day, 1hr, 3hr or 6hr
Returns
-------
iris.cube.Cube
cube with converted time axis and units.
"""
# standardize time points
coord = cube.coord('time')
time_c = coord.units.num2date(coord.points)
if frequency == 'yr':
time_cells = [datetime.datetime(t.year, 7, 1, 0, 0, 0) for t in time_c]
elif frequency == 'mon':
time_cells = [
datetime.datetime(t.year, t.month, 15, 0, 0, 0) for t in time_c
]
elif frequency == 'day':
time_cells = [
datetime.datetime(t.year, t.month, t.day, 0, 0, 0) for t in time_c
]
elif frequency == '1hr':
time_cells = [
datetime.datetime(t.year, t.month, t.day, t.hour, 0, 0)
for t in time_c
]
elif frequency == '3hr':
time_cells = [
datetime.datetime(
t.year, t.month, t.day, t.hour - t.hour % 3, 0, 0)
for t in time_c
]
elif frequency == '6hr':
time_cells = [
datetime.datetime(
t.year, t.month, t.day, t.hour - t.hour % 6, 0, 0)
for t in time_c
]
coord = cube.coord('time')
cube.coord('time').points = date2num(time_cells, coord.units, coord.dtype)
# uniformize bounds
cube.coord('time').bounds = None
cube.coord('time').bounds = _get_time_bounds(cube.coord('time'), frequency)
# remove aux coords that will differ
reset_aux = ['day_of_month', 'day_of_year']
for auxcoord in cube.aux_coords:
if auxcoord.long_name in reset_aux:
cube.remove_coord(auxcoord)
# re-add the converted aux coords
iris.coord_categorisation.add_day_of_month(cube,
cube.coord('time'),
name='day_of_month')
iris.coord_categorisation.add_day_of_year(cube,
cube.coord('time'),
name='day_of_year')
return cube
def low_pass_weights(window, cutoff):
"""Calculate weights for a low pass Lanczos filter.
Method borrowed from `iris example
<https://scitools-iris.readthedocs.io/en/latest/generated/gallery/general/plot_SOI_filtering.html?highlight=running%20mean>`_
Parameters
----------
window: int
The length of the filter window.
cutoff: float
The cutoff frequency in inverse time steps.
Returns
-------
list:
List of floats representing the weights.
"""
order = ((window - 1) // 2) + 1
nwts = 2 * order + 1
weights = np.zeros([nwts])
half_order = nwts // 2
weights[half_order] = 2 * cutoff
kidx = np.arange(1., half_order)
sigma = np.sin(np.pi * kidx / half_order) * half_order / (np.pi * kidx)
firstfactor = np.sin(2. * np.pi * cutoff * kidx) / (np.pi * kidx)
weights[(half_order - 1):0:-1] = firstfactor * sigma
weights[(half_order + 1):-1] = firstfactor * sigma
return weights[1:-1]