@@ -105,7 +105,7 @@ def test_regroup_daily_to_7D():
105
105
t = pd .date_range (start = "2000-01-01" , end = "2000-01-28" , freq = "1D" )
106
106
values = 1 + np .arange (t .size )
107
107
precip = xr .DataArray (values , coords = {"T" : t })
108
- precip_int = calc .regroup (precip , group = "7D" , method = " sum" )
108
+ precip_int = calc .regroup (precip , group = "7D" ). sum ( )
109
109
110
110
np .testing .assert_array_equal (
111
111
precip_int , precip .resample (T = "7D" ).sum (skipna = True , min_count = 7 ).dropna ("T" )
@@ -121,8 +121,8 @@ def test_regroup_daily_to_pentad():
121
121
values_leap = 1 + np .arange (t_leap .size )
122
122
precip = xr .DataArray (values , coords = {"T" : t })
123
123
precip_leap = xr .DataArray (values_leap , coords = {"T" : t_leap })
124
- precip_pentad = calc .regroup (precip , group = "pentad" , method = " sum" )
125
- precip_pentad_leap = calc .regroup (precip_leap , group = "pentad" , method = " sum" )
124
+ precip_pentad = calc .regroup (precip , group = "pentad" ). sum ( )
125
+ precip_pentad_leap = calc .regroup (precip_leap , group = "pentad" ). sum ( )
126
126
127
127
np .testing .assert_array_equal (precip_pentad .data , [
128
128
15. , 40. , 65. , 90. , 115. , 140. , 165. , 190. , 215. , 240. , 265. ,
@@ -141,7 +141,7 @@ def test_regroup_daily_to_8day():
141
141
t = pd .date_range (start = "2020-11-01T120000" , end = "2021-02-01T120000" , freq = "1D" )
142
142
values = 1 + np .arange (t .size )
143
143
precip = xr .DataArray (values , coords = {"T" : t })
144
- precip_8day = calc .regroup (precip , group = "8day" , method = " sum" )
144
+ precip_8day = calc .regroup (precip , group = "8day" ). sum ( )
145
145
146
146
np .testing .assert_array_equal (precip_8day .data , [
147
147
92. , 156. , 220. , 284. , 348. , 412. , 351. , 524. , 588. , 652. , 716.
@@ -152,7 +152,7 @@ def test_regroup_daily_to_dekad():
152
152
t = pd .date_range (start = "2020-01-01T120000" , end = "2020-03-09T120000" , freq = "1D" )
153
153
values = 1 + np .arange (t .size )
154
154
precip = xr .DataArray (values , coords = {"T" : t })
155
- precip_dekad = calc .regroup (precip , group = "dekad" , method = " sum" )
155
+ precip_dekad = calc .regroup (precip , group = "dekad" ). sum ( )
156
156
157
157
np .testing .assert_array_equal (
158
158
precip_dekad .data , [55. , 155. , 286. , 365. , 465. , 504. ]
@@ -163,7 +163,7 @@ def test_regroup_daily_to_16day():
163
163
t = pd .date_range (start = "2000-11-01T120000" , end = "2001-02-01T120000" , freq = "1D" )
164
164
values = 1 + np .arange (t .size )
165
165
precip = xr .DataArray (values , coords = {"T" : t })
166
- precip_16day = calc .regroup (precip , group = "16day" , method = " sum" )
166
+ precip_16day = calc .regroup (precip , group = "16day" ). sum ( )
167
167
168
168
np .testing .assert_array_equal (
169
169
precip_16day .data , [376. , 632. , 763. , 1112. , 1368. ]
@@ -174,7 +174,7 @@ def test_regroup_daily_to_1M():
174
174
t = pd .date_range (start = "2000-11-01T120000" , end = "2001-02-01T120000" , freq = "1D" )
175
175
values = 1 + np .arange (t .size )
176
176
precip = xr .DataArray (values , coords = {"T" : t })
177
- precip_month = calc .regroup (precip , group = "1M" , method = " sum" )
177
+ precip_month = calc .regroup (precip , group = "1M" ). sum ( )
178
178
179
179
np .testing .assert_array_equal (
180
180
precip_month .data ,
@@ -186,7 +186,7 @@ def test_regroup_daily_to_5M():
186
186
t = pd .date_range (start = "2000-01-01" , end = "2001-12-31" , freq = "1D" )
187
187
values = 1 + np .arange (t .size )
188
188
precip = xr .DataArray (values , coords = {"T" : t })
189
- precip_month = calc .regroup (precip , group = "5M" , method = " sum" )
189
+ precip_month = calc .regroup (precip , group = "5M" ). sum ( )
190
190
191
191
xr .testing .assert_equal (
192
192
precip_month .isel (T_bins = 0 , drop = True ),
@@ -202,7 +202,7 @@ def test_regroup_daily_to_season1():
202
202
t = pd .date_range (start = "2000-01-01" , end = "2002-12-31" , freq = "1D" )
203
203
values = 1 + np .arange (t .size )
204
204
precip = xr .DataArray (values , coords = {"T" : t })
205
- precip_seas = calc .regroup (precip , group = "14 Dec - 29 Mar" , method = " sum" )
205
+ precip_seas = calc .regroup (precip , group = "14 Dec - 29 Mar" ). sum ( )
206
206
207
207
xr .testing .assert_equal (
208
208
precip_seas .isel (T_bins = 0 , drop = True ),
@@ -230,7 +230,7 @@ def test_regroup_daily_to_season2():
230
230
t = pd .date_range (start = "2000-01-01" , end = "2001-12-31" , freq = "1D" )
231
231
values = 1 + np .arange (t .size )
232
232
precip = xr .DataArray (values , coords = {"T" : t })
233
- precip_seas = calc .regroup (precip , group = "19-29 Feb" , method = " sum" )
233
+ precip_seas = calc .regroup (precip , group = "19-29 Feb" ). sum ( )
234
234
235
235
xr .testing .assert_equal (
236
236
precip_seas .isel (T_bins = 0 , drop = True ),
@@ -246,7 +246,7 @@ def test_regroup_daily_to_season3():
246
246
t = pd .date_range (start = "2000-01-01" , end = "2001-12-31" , freq = "1D" )
247
247
values = 1 + np .arange (t .size )
248
248
precip = xr .DataArray (values , coords = {"T" : t })
249
- precip_seas = calc .regroup (precip , group = "29 Feb - 29 Mar" , method = " sum" )
249
+ precip_seas = calc .regroup (precip , group = "29 Feb - 29 Mar" ). sum ( )
250
250
251
251
xr .testing .assert_equal (
252
252
precip_seas .isel (T_bins = 0 , drop = True ),
@@ -262,7 +262,7 @@ def test_regroup_daily_to_int():
262
262
t = pd .date_range (start = "2000-01-01" , end = "2000-01-28" , freq = "1D" )
263
263
values = 1 + np .arange (t .size )
264
264
precip = xr .DataArray (values , coords = {"T" : t })
265
- precip_int = calc .regroup (precip , group = 4 , method = " sum" )
265
+ precip_int = calc .regroup (precip , group = 4 ). sum ( )
266
266
267
267
np .testing .assert_array_equal (
268
268
precip_int ,
@@ -274,7 +274,7 @@ def test_resample_interval_to_daily():
274
274
t = pd .date_range (start = "2000-01-01" , end = "2000-01-28" , freq = "1D" )
275
275
values = 1 + np .arange (t .size )
276
276
precip = xr .DataArray (values , coords = {"T" : t })
277
- precip_pentad = calc .regroup (precip , group = "pentad" , method = " sum" )
277
+ precip_pentad = calc .regroup (precip , group = "pentad" ). sum ( )
278
278
precip_daily = calc .resample_interval_to_daily (precip_pentad )
279
279
280
280
np .testing .assert_array_equal (precip_daily , [
@@ -287,7 +287,7 @@ def test_resample_interval_to_daily_intensive():
287
287
t = pd .date_range (start = "2000-01-01" , end = "2000-01-28" , freq = "1D" )
288
288
values = 1 + np .arange (t .size )
289
289
precip = xr .DataArray (values , coords = {"T" : t }, attrs = {"units" : "mm/day" })
290
- precip_pentad = calc .regroup (precip , group = "pentad" , method = " mean" )
290
+ precip_pentad = calc .regroup (precip , group = "pentad" ). mean ( )
291
291
precip_daily = calc .resample_interval_to_daily (precip_pentad )
292
292
293
293
np .testing .assert_array_equal (precip_daily , [
@@ -630,6 +630,37 @@ def test_seasonal_onset_date_keeps_returning_same_outputs():
630
630
),
631
631
)
632
632
633
+ def test_seasonal_onset_date_keeps_returning_same_outputs_with_regroup ():
634
+ precip = data_test_calc .multi_year_data_sample ()
635
+ onsetsds = calc .regroup (
636
+ time_series = precip , group = "1 Mar - 20 Jun"
637
+ ).map (calc .onset_date , ** {
638
+ "wet_thresh" : 1 ,
639
+ "wet_spell_length" : 3 ,
640
+ "wet_spell_thresh" : 20 ,
641
+ "min_wet_days" : 1 ,
642
+ "dry_spell_length" : 7 ,
643
+ "dry_spell_search" : 21 ,
644
+ })
645
+ # That part could be included in regroup for this specific group case
646
+ onsetsds = onsetsds .isel (T_bins = np .arange (0 , onsetsds .size , 2 ), drop = True )
647
+ # Note that onset_date is written and such a manner that is also outputs T, while
648
+ # regroup brings intervals T_bins. T_bins is enough so onset_date could possibly
649
+ # be rewritten accordingly
650
+ onsets = (onsetsds + onsetsds ["T" ])
651
+
652
+ np .testing .assert_array_equal (
653
+ onsets ,
654
+ pd .to_datetime (
655
+ [
656
+ "NaT" ,
657
+ "2001-03-08T00:00:00.000000000" ,
658
+ "NaT" ,
659
+ "2003-04-12T00:00:00.000000000" ,
660
+ "2004-04-04T00:00:00.000000000" ,
661
+ ],
662
+ ),
663
+ )
633
664
634
665
def test_seasonal_cess_date_keeps_returning_same_outputs ():
635
666
@@ -663,6 +694,38 @@ def test_seasonal_cess_date_keeps_returning_same_outputs():
663
694
),
664
695
)
665
696
697
+ def test_seasonal_cess_date_keeps_returning_same_outputs_with_regroup ():
698
+
699
+ precip = data_test_calc .multi_year_data_sample ()
700
+ wb = calc .water_balance (
701
+ daily_rain = precip ,
702
+ et = 5 ,
703
+ taw = 60 ,
704
+ sminit = 0 ,
705
+ time_dim = "T"
706
+ ).to_array (name = "soil moisture" ).squeeze ("variable" , drop = True )
707
+ cessds = calc .regroup (
708
+ time_series = wb , group = "1 Sep - 30 Nov"
709
+ ).map (calc .cess_date_from_sm , ** {
710
+ "dry_thresh" : 5 ,
711
+ "dry_spell_length_thresh" : 3 ,
712
+ })
713
+ # Not sure what happened to T_bins here
714
+ cessds = cessds .isel (T = np .arange (0 , cessds .size , 2 ), drop = True )
715
+ cess = (cessds + cessds ["T" ]).squeeze ()
716
+ np .testing .assert_array_equal (
717
+ cess ,
718
+ pd .to_datetime (
719
+ [
720
+ "2000-09-21T00:00:00.000000000" ,
721
+ "2001-09-03T00:00:00.000000000" ,
722
+ "2002-09-03T00:00:00.000000000" ,
723
+ "2003-09-24T00:00:00.000000000" ,
724
+ "2004-09-01T00:00:00.000000000" ,
725
+ ],
726
+ ),
727
+ )
728
+
666
729
667
730
def test_seasonal_cess_date_from_rain_keeps_returning_same_outputs ():
668
731
@@ -683,6 +746,24 @@ def test_seasonal_cess_date_from_rain_keeps_returning_same_outputs():
683
746
assert cess [0 ] == pd .to_datetime ("2000-09-21T00:00:00.000000000" )
684
747
685
748
749
+ def test_seasonal_cess_date_from_rain_keeps_returning_same_outputs_with_regroup ():
750
+
751
+ precip = data_test_calc .multi_year_data_sample ()
752
+ cessds = calc .regroup (
753
+ time_series = precip , group = "1 Sep - 30 Nov"
754
+ ).map (calc .cess_date_from_rain , ** {
755
+ "dry_thresh" : 5 ,
756
+ "dry_spell_length_thresh" : 3 ,
757
+ "et" : 5 ,
758
+ "taw" : 60 ,
759
+ "sminit" : 33.57026932 , # from previous test sm output on 8/31/2000
760
+ })
761
+ cessds = cessds .isel (T = np .arange (0 , cessds .size , 2 ), drop = True )
762
+ cess = (cessds + cessds ["T" ]).squeeze ()
763
+
764
+ assert cess [0 ] == pd .to_datetime ("2000-09-21T00:00:00.000000000" )
765
+
766
+
686
767
def test_seasonal_onset_date ():
687
768
t = pd .date_range (start = "2000-01-01" , end = "2005-02-28" , freq = "1D" )
688
769
# this is rr_mrg.sel(T=slice("2000", "2005-02-28")).isel(X=150, Y=150).precip
@@ -764,19 +845,15 @@ def test_seasonal_onset_date_with_regroup():
764
845
).rename ("synthetic_precip" )
765
846
766
847
onsetsds = calc .regroup (
767
- time_series = synthetic_precip ,
768
- group = "1 Mar - 20 Jun" ,
769
- method = "map" ,
770
- method_kwargs = {
771
- "func" : calc .onset_date ,
848
+ time_series = synthetic_precip , group = "1 Mar - 20 Jun"
849
+ ).map (calc .onset_date , ** {
772
850
"wet_thresh" : 1 ,
773
851
"wet_spell_length" : 3 ,
774
852
"wet_spell_thresh" : 20 ,
775
853
"min_wet_days" : 1 ,
776
854
"dry_spell_length" : 7 ,
777
855
"dry_spell_search" : 21 ,
778
- },
779
- )
856
+ })
780
857
onsetsds = onsetsds .isel (T_bins = np .arange (0 , onsetsds .size , 2 ), drop = True )
781
858
onsets = (onsetsds + onsetsds ["T" ]).drop_vars ("T" )
782
859
@@ -885,15 +962,11 @@ def test_seasonal_cess_date_with_regroup():
885
962
time_dim = "T"
886
963
).to_array (name = "soil moisture" )
887
964
cessds = calc .regroup (
888
- time_series = wb ,
889
- group = "1 Sep - 30 Nov" ,
890
- method = "map" ,
891
- method_kwargs = {
892
- "func" : calc .cess_date_from_sm ,
965
+ time_series = wb , group = "1 Sep - 30 Nov" ,
966
+ ).map (calc .cess_date_from_sm , ** {
893
967
"dry_thresh" : 5 ,
894
968
"dry_spell_length_thresh" : 3 ,
895
- },
896
- )
969
+ })
897
970
cessds = cessds .isel (T = np .arange (0 , cessds .size , 2 ), drop = True )
898
971
cess = (cessds + cessds ["T" ]).squeeze (drop = True )
899
972
@@ -987,18 +1060,14 @@ def test_seasonal_cess_date_from_rain_with_regroup():
987
1060
synthetic_precip ,
988
1061
).rename ("synthetic_precip" )
989
1062
cessds = calc .regroup (
990
- time_series = synthetic_precip ,
991
- group = "1 Sep - 30 Nov" ,
992
- method = "map" ,
993
- method_kwargs = {
994
- "func" : calc .cess_date_from_rain ,
1063
+ time_series = synthetic_precip , group = "1 Sep - 30 Nov"
1064
+ ).map (calc .cess_date_from_rain , ** {
995
1065
"dry_thresh" : 5 ,
996
1066
"dry_spell_length_thresh" : 3 ,
997
1067
"et" : 5 ,
998
1068
"taw" : 60 ,
999
1069
"sminit" : 0 ,
1000
- },
1001
- )
1070
+ })
1002
1071
cessds = cessds .isel (T = np .arange (0 , cessds .size , 2 ), drop = True )
1003
1072
cess = (cessds + cessds ["T" ]).squeeze ()
1004
1073
0 commit comments