Allow SLR to be generated with an external function

master
Dan Howe 3 years ago
parent faa3ef5d24
commit 1e8a2b5714

@ -169,6 +169,7 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
'min' (array_like): minimum value 'min' (array_like): minimum value
'mode' (array_like): most likely value 'mode' (array_like): most likely value
'max' (array_like): maximum value 'max' (array_like): maximum value
'function' (str): optional external function ('package.function')
bruun_factor (dict): bruun_factor (dict):
'min' (float): minimum value 'min' (float): minimum value
'mode' (float): most likely value 'mode' (float): most likely value
@ -180,6 +181,12 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
Returns: Returns:
the simulated recession distance (m) the simulated recession distance (m)
Notes:
'sea_level_rise' is calculated with a triangular probability distribution
by default. Alternatively 'sea_level_rise' can be calculated using an
external function, to which the arguments 'n_runs', 'start_year', and
'end_year' are passed.
""" """
# Get time interval from input file # Get time interval from input file
@ -187,45 +194,54 @@ def get_ongoing_recession(n_runs, start_year, end_year, sea_level_rise,
n_years = len(years) n_years = len(years)
# Interpolate sea level rise projections (m) # Interpolate sea level rise projections (m)
slr_mode = np.interp(years, if sea_level_rise['function']: # Get slr from separate function
xp=sea_level_rise['year'], # Get names of package/script and function
fp=sea_level_rise['mode'])[:, np.newaxis] pkg, func_name = sea_level_rise['function'].split('.')
try: # Import function from package
slr_min = np.interp(years, func = getattr(__import__(pkg), func_name)
xp=sea_level_rise['year'], slr = func(n_runs=n_runs, start_year=start_year, end_year=end_year)
fp=sea_level_rise['min'])[:, np.newaxis]
except ValueError: else: # Use triangular distribution
# Use mode for deterministic beaches slr_mode = np.interp(years,
slr_min = slr_mode xp=sea_level_rise['year'],
fp=sea_level_rise['mode'])[:, np.newaxis]
try:
slr_max = np.interp(years,
xp=sea_level_rise['year'],
fp=sea_level_rise['max'])[:, np.newaxis]
except ValueError:
# Use mode for deterministic beaches
slr_max = slr_mode
# Initialise sea level rise array
slr = np.zeros([n_years, n_runs])
for i in range(n_years):
# Use triangular distribution for SLR in each year (m)
try: try:
slr[i, :] = np.random.triangular(left=slr_min[i], slr_min = np.interp(years,
mode=slr_mode[i], xp=sea_level_rise['year'],
right=slr_max[i], fp=sea_level_rise['min'])[:, np.newaxis]
size=n_runs)
except ValueError: except ValueError:
# Use constant value if slr_min == slr_max # Use mode for deterministic beaches
slr[i, :] = np.ones([1, n_runs]) * slr_mode[i] slr_min = slr_mode
# Sort each row, so SLR follows a smooth trajectory for each model run try:
slr = np.sort(slr, axis=1) slr_max = np.interp(years,
xp=sea_level_rise['year'],
# Shuffle columns, so the order of model runs is randomised fp=sea_level_rise['max'])[:, np.newaxis]
slr = np.random.permutation(slr.T).T except ValueError:
# Use mode for deterministic beaches
slr_max = slr_mode
# Initialise sea level rise array
slr = np.zeros([n_years, n_runs])
for i in range(n_years):
# Use triangular distribution for SLR in each year (m)
try:
slr[i, :] = np.random.triangular(left=slr_min[i],
mode=slr_mode[i],
right=slr_max[i],
size=n_runs)
except ValueError:
# Use constant value if slr_min == slr_max
slr[i, :] = np.ones([1, n_runs]) * slr_mode[i]
# Sort each row, so SLR follows a smooth trajectory for each model run
slr = np.sort(slr, axis=1)
# Shuffle columns, so the order of model runs is randomised
slr = np.random.permutation(slr.T).T
# Shift sea level so it is zero in the start year # Shift sea level so it is zero in the start year
slr -= slr[0, :].mean(axis=0) slr -= slr[0, :].mean(axis=0)

Loading…
Cancel
Save