2727ensemble = "r1i1p1f1" # <- note that this is not used in the script
2828year_start = 1990
2929num_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
154155def climatology (ds , lumpby ):
155156 if lumpby == "month" :
0 commit comments