Skip to content

Commit 81ad0ec

Browse files
committed
Save mean number of days per month in monthly climatologies
1 parent f666c45 commit 81ad0ec

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

scripts/cyclo_average_CMIP5_ACCESS_variables.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ def season_climatology(ds):
124124
# Make a DataArray with the number of days in each month, size = len(time)
125125
month_length = ds.time.dt.days_in_month
126126
# Calculate the weights by grouping by 'time.season'
127-
weights = (
128-
month_length.groupby("time.season") / month_length.groupby("time.season").sum()
129-
)
127+
weights = month_length.groupby("time.season") / month_length.groupby("time.season").sum()
130128
# Test that the sum of the weights for each season is 1.0
131129
np.testing.assert_allclose(weights.groupby("time.season").sum().values, np.ones(4))
132130
# Calculate the weighted average
@@ -136,13 +134,16 @@ def month_climatology(ds):
136134
# Make a DataArray with the number of days in each month, size = len(time)
137135
month_length = ds.time.dt.days_in_month
138136
# Calculate the weights by grouping by 'time.season'
139-
weights = (
140-
month_length.groupby("time.month") / month_length.groupby("time.month").sum()
141-
)
137+
weights = month_length.groupby("time.month") / month_length.groupby("time.month").sum()
142138
# Test that the sum of the weights for each month is 1.0
143139
np.testing.assert_allclose(weights.groupby("time.month").sum().values, np.ones(12))
144140
# Calculate the weighted average
145-
return (ds * weights).groupby("time.month").sum(dim="time")
141+
ds_out = (ds * weights).groupby("time.month").sum(dim="time")
142+
# Keep track of mean number of days per month
143+
mean_days_in_month = month_length.groupby("time.month").mean()
144+
# And assign it to new coordinate
145+
ds_out = ds_out.assign_coords(mean_days_in_month=('month', mean_days_in_month.data))
146+
return ds_out
146147

147148
def climatology(ds, lumpby):
148149
if lumpby == "month":

scripts/cyclo_average_CMIP6_ACCESS_variables.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ensemble="r1i1p1f1" # <- note that this is not used in the script
2828
year_start=1990
2929
num_years=10
30+
lumpby="month"
3031

3132

3233
# Model etc. defined from script input
@@ -107,7 +108,6 @@ def select_latest_cat(cat, **kwargs):
107108
# if dataframe is empty, error
108109
if selectedcat.df.empty:
109110
raise ValueError(f"No data found for {kwargs}")
110-
111111
latestselectedcat = selectedcat.search(version=find_latest_version(selectedcat))
112112
return latestselectedcat
113113

@@ -131,9 +131,7 @@ def season_climatology(ds):
131131
# Make a DataArray with the number of days in each month, size = len(time)
132132
month_length = ds.time.dt.days_in_month
133133
# Calculate the weights by grouping by 'time.season'
134-
weights = (
135-
month_length.groupby("time.season") / month_length.groupby("time.season").sum()
136-
)
134+
weights = month_length.groupby("time.season") / month_length.groupby("time.season").sum()
137135
# Test that the sum of the weights for each season is 1.0
138136
np.testing.assert_allclose(weights.groupby("time.season").sum().values, np.ones(4))
139137
# Calculate the weighted average
@@ -143,13 +141,16 @@ def month_climatology(ds):
143141
# Make a DataArray with the number of days in each month, size = len(time)
144142
month_length = ds.time.dt.days_in_month
145143
# Calculate the weights by grouping by 'time.season'
146-
weights = (
147-
month_length.groupby("time.month") / month_length.groupby("time.month").sum()
148-
)
144+
weights = month_length.groupby("time.month") / month_length.groupby("time.month").sum()
149145
# Test that the sum of the weights for each month is 1.0
150146
np.testing.assert_allclose(weights.groupby("time.month").sum().values, np.ones(12))
151147
# Calculate the weighted average
152-
return (ds * weights).groupby("time.month").sum(dim="time")
148+
ds_out = (ds * weights).groupby("time.month").sum(dim="time")
149+
# Keep track of mean number of days per month
150+
mean_days_in_month = month_length.groupby("time.month").mean()
151+
# And assign it to new coordinate
152+
ds_out = ds_out.assign_coords(mean_days_in_month=('month', mean_days_in_month.data))
153+
return ds_out
153154

154155
def climatology(ds, lumpby):
155156
if lumpby == "month":

scripts/cyclo_average_unarchived_CMIP6_ACCESS_GM_variables.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def season_climatology(ds):
101101
# Make a DataArray with the number of days in each month, size = len(time)
102102
month_length = ds.time.dt.days_in_month
103103
# Calculate the weights by grouping by 'time.season'
104-
weights = (
105-
month_length.groupby("time.season") / month_length.groupby("time.season").sum()
106-
)
104+
weights = month_length.groupby("time.season") / month_length.groupby("time.season").sum()
107105
# Test that the sum of the weights for each season is 1.0
108106
np.testing.assert_allclose(weights.groupby("time.season").sum().values, np.ones(4))
109107
# Calculate the weighted average
@@ -113,13 +111,16 @@ def month_climatology(ds):
113111
# Make a DataArray with the number of days in each month, size = len(time)
114112
month_length = ds.time.dt.days_in_month
115113
# Calculate the weights by grouping by 'time.season'
116-
weights = (
117-
month_length.groupby("time.month") / month_length.groupby("time.month").sum()
118-
)
114+
weights = month_length.groupby("time.month") / month_length.groupby("time.month").sum()
119115
# Test that the sum of the weights for each month is 1.0
120116
np.testing.assert_allclose(weights.groupby("time.month").sum().values, np.ones(12))
121117
# Calculate the weighted average
122-
return (ds * weights).groupby("time.month").sum(dim="time")
118+
ds_out = (ds * weights).groupby("time.month").sum(dim="time")
119+
# Keep track of mean number of days per month
120+
mean_days_in_month = month_length.groupby("time.month").mean()
121+
# And assign it to new coordinate
122+
ds_out = ds_out.assign_coords(mean_days_in_month=('month', mean_days_in_month.data))
123+
return ds_out
123124

124125
def climatology(ds, lumpby):
125126
if lumpby == "month":

0 commit comments

Comments
 (0)