Skip to content

Commit

Permalink
Merge 3ee0ddc into 0fa0958
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranAngevaare committed Aug 9, 2023
2 parents 0fa0958 + 3ee0ddc commit 933e5d7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions optim_esm_tools/analyze/cmip_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def add_conditions_to_ds(
calculate_conditions = (
tipping_criteria.StartEndDifference,
tipping_criteria.StdDetrended,
tipping_criteria.StdDetrendedYearly,
tipping_criteria.MaxJump,
tipping_criteria.MaxJumpYearly,
tipping_criteria.MaxDerivitive,
tipping_criteria.MaxJumpAndStd,
)
Expand Down
2 changes: 1 addition & 1 deletion optim_esm_tools/analyze/region_finding.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def store_mask(self, mask, m_i, store_masks=True):
if k not in 'max_time min_time'.split()
}
statistics = TimeStatistics(
ds_masked, calculation_kwargs=dict(max_jump=kw)
ds_masked, calculation_kwargs=dict(max_jump=kw, n_std_global=kw)
).calculate_statistics()
ds_masked.attrs.update(statistics)
ds_masked.to_netcdf(
Expand Down
11 changes: 8 additions & 3 deletions optim_esm_tools/analyze/time_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import xarray as xr
import typing as ty
from functools import partial
import os


class TimeStatistics:
Expand Down Expand Up @@ -71,7 +72,11 @@ def n_times_global_std(ds, field='std detrended', average_over=None, **read_kw):
average_over = average_over or oet.config.config['analyze']['lon_lat_dim'].split(
','
)
ds_global = oet.load_glob(ds.attrs['file'], **read_kw)
path = ds.attrs['file']
if os.path.exists(path):
ds_global = oet.load_glob(path)
else:
ds_global = oet.read_ds(os.path.split(path)[0], **read_kw)
return float(ds[field].mean(average_over) / ds_global[field].mean(average_over))


Expand Down Expand Up @@ -145,7 +150,7 @@ def calculate_symmetry_test(ds, field=None, nan_policy='omit'):


def calculate_max_jump_in_std_vs_history(
ds, field='max jump', field_pi_control='std detrended', **kw
ds, field='max jump yearly', field_pi_control='std detrended yearly', **kw
):
ds_hist = get_historical_ds(ds, **kw)
if ds_hist is None:
Expand All @@ -161,4 +166,4 @@ def calculate_max_jump_in_std_vs_history(
cur = cur[~isnnan]
his = his[~isnnan]

return np.median(cur / his)
return np.mean(cur) / np.mean(his)
37 changes: 35 additions & 2 deletions optim_esm_tools/analyze/tipping_criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ class StdDetrended(_Condition):
def long_description(self):
return f'Standard deviation of running mean ({self.running_mean} yr). Detrended'

@property
def use_variable(self):
return '{variable}_detrend_run_mean_{running_mean}'

def calculate(self, data_set):
return running_mean_std(
data_set,
variable=self.variable,
time_var=self.time_var,
naming='{variable}_detrend_run_mean_{running_mean}',
naming=self.use_variable,
running_mean=self.running_mean,
**self.defaults,
)
Expand All @@ -85,18 +89,47 @@ def __init__(self, *args, **kwargs):
def long_description(self):
return f'Max change in {self.number_of_years} yr in the running mean ({self.running_mean} yr). Not detrended'

@property
def use_variable(self):
return '{variable}_run_mean_{running_mean}'

def calculate(self, data_set):
return max_change_xyr(
data_set,
variable=self.variable,
time_var=self.time_var,
naming='{variable}_run_mean_{running_mean}',
naming=self.use_variable,
x_yr=self.number_of_years,
running_mean=self.running_mean,
**self.defaults,
)


class MaxJumpYearly(MaxJump):
short_description: str = 'max jump yearly'

def __init__(self, *args, **kwargs):
kwargs['running_mean'] = 1
super().__init__(*args, **kwargs)

@property
def use_variable(self):
assert self.running_mean == 1
return '{variable}'


class StdDetrendedYearly(StdDetrended):
short_description: str = 'std detrended yearly'

@property
def long_description(self):
return f'Standard deviation. Detrended'

@property
def use_variable(self):
return '{variable}_detrend'


class MaxDerivitive(_Condition):
short_description: str = 'max derivative'

Expand Down
2 changes: 1 addition & 1 deletion optim_esm_tools/optim_esm_conf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ seconds_to_year = 31557600


[versions]
cmip_handler = 0.4.5
cmip_handler = 0.4.6


[display]
Expand Down

0 comments on commit 933e5d7

Please sign in to comment.