Skip to content

Commit

Permalink
First renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion committed Mar 2, 2023
1 parent 3691134 commit 1180d52
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 101 deletions.
2 changes: 1 addition & 1 deletion oggm/cli/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def run_benchmark(rgi_version=None, rgi_reg=None, border=None,
tasks.catchment_intersections,
tasks.catchment_width_geom,
tasks.catchment_width_correction,
tasks.mb_calibration_from_geodetic_mb,
tasks.mb_calibration_from_scalar_mb,
tasks.apparent_mb_from_any_mb,
tasks.prepare_for_inversion,
tasks.mass_conservation_inversion,
Expand Down
4 changes: 2 additions & 2 deletions oggm/cli/prepro_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ def _time_log():

utils.get_geodetic_mb_dataframe() # Small optim to avoid concurrency
if mb_calibration_strategy == 'melt_temp':
workflow.execute_entity_task(tasks.mb_calibration_from_geodetic_mb,
workflow.execute_entity_task(tasks.mb_calibration_from_scalar_mb,
gdirs,
calibrate_param1='melt_f',
calibrate_param2='temp_bias')
elif mb_calibration_strategy == 'temp_melt':
workflow.execute_entity_task(tasks.mb_calibration_from_geodetic_mb,
workflow.execute_entity_task(tasks.mb_calibration_from_scalar_mb,
gdirs,
calibrate_param1='temp_bias',
calibrate_param2='melt_f')
Expand Down
90 changes: 57 additions & 33 deletions oggm/core/massbalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,52 +1409,53 @@ def calving_mb(gdir):


@entity_task(log, writes=['mb_calib'], fallback=_fallback_mb_calibration)
def mb_calibration_from_insitu_mb(gdir, **kwargs):
def mb_calibration_from_wgms_mb(gdir, **kwargs):
"""Calibrate for in-situ, annual MB.
For now this just calls mb_calibration_from_geodetic_mb internally,
This only works for glaciers which have WGMS data!
For now this just calls mb_calibration_from_scalar_mb internally,
but could be cleverer than that if someone wishes to implement it.
Parameters
----------
**kwargs : any kwarg accepted by mb_calibration_from_geodetic_mb
**kwargs : any kwarg accepted by mb_calibration_from_scalar_mb
"""

# Note that this currently does not work for hydro years (WGMS uses hydro)
# A way to go would be to teach the mb models to use calendar years
# internally but still output annual MB in hydro convention.
mbdf = gdir.get_ref_mb_data()
ref_mb = mbdf.ANNUAL_BALANCE.mean()
ref_period = f'{mbdf.index[0]}-01-01_{mbdf.index[-1] + 1}-01-01'
# TODO: this could be wrong if not all years are there!
mb_calibration_from_geodetic_mb(gdir,
ref_mb=ref_mb,
ref_period=ref_period,
**kwargs)
# Keep only valid values
mbdf = mbdf.loc[~mbdf['ANNUAL_BALANCE'].isnull()]
mb_calibration_from_scalar_mb(gdir,
ref_mb=mbdf['ANNUAL_BALANCE'].mean(),
ref_mb_years=mbdf.index.values,
**kwargs)


@entity_task(log, writes=['mb_calib'], fallback=_fallback_mb_calibration)
def mb_calibration_from_geodetic_mb(gdir,
ref_mb=None,
ref_period='',
ref_mb_years=None,
write_to_gdir=True,
overwrite_gdir=False,
override_missing=None,
calibrate_param1='melt_f',
calibrate_param2=None,
calibrate_param3=None,
melt_f_default=None,
melt_f_min=None,
melt_f_max=None,
prcp_scaling_factor=None,
prcp_scaling_factor_min=None,
prcp_scaling_factor_max=None,
temp_bias=0,
temp_bias_min=None,
temp_bias_max=None,
mb_model_class=MonthlyTIModel,
):
def mb_calibration_from_scalar_mb(gdir,
ref_mb=None,
ref_period=None,
ref_mb_years=None,
write_to_gdir=True,
overwrite_gdir=False,
override_missing=None,
calibrate_param1='melt_f',
calibrate_param2=None,
calibrate_param3=None,
melt_f_default=None,
melt_f_min=None,
melt_f_max=None,
prcp_scaling_factor=None,
prcp_scaling_factor_min=None,
prcp_scaling_factor_max=None,
temp_bias=0,
temp_bias_min=None,
temp_bias_max=None,
mb_model_class=MonthlyTIModel,
):
"""Determine the mass balance parameters from a scalar mass-balance value.
This calibrates the mass balance parameters using a reference average
Expand All @@ -1481,6 +1482,29 @@ def mb_calibration_from_geodetic_mb(gdir,
given, all years between this range (excluding the last one) are used.
If a list of years is given, all these will be used (useful for
data with gaps)
write_to_gdir : bool
whether to write the results of the calibration to the glacier
directory. If True (the default), this will be saved as `mb_calib.json`
and be used by the MassBalanceModel class as parameters in subsequent
tasks.
overwrite_gdir : bool
if a `mb_calib.json` exists, this task won't overwrite it per default.
Set this to True to enforce overwriting (i.e. with consequences for the
future workflow).
override_missing : scalar
if the reference geodetic data is not available
calibrate_param1='melt_f',
calibrate_param2=None,
calibrate_param3=None,
melt_f_default=None,
melt_f_min=None,
melt_f_max=None,
prcp_scaling_factor=None,
prcp_scaling_factor_min=None,
prcp_scaling_factor_max=None,
temp_bias=0,
temp_bias_min=None,
temp_bias_max=None,
mb_model_class : MassBalanceModel class
the MassBalanceModel to use for the calibration
default is MonthlyTIModel
Expand All @@ -1501,15 +1525,15 @@ def mb_calibration_from_geodetic_mb(gdir,
temp_bias_min = cfg.PARAMS['temp_bias_min']
if temp_bias_max is None:
temp_bias_max = cfg.PARAMS['temp_bias_max']
if ref_mb_years and ref_period:
if ref_mb_years is not None and ref_period is not None:
raise InvalidParamsError('Cannot set `ref_mb_years` and `ref_period` '
'at the same time.')

fls = gdir.read_pickle('inversion_flowlines')

# Let's go
# Climate period
if ref_mb_years:
if ref_mb_years is not None:
if len(ref_mb_years) > 2:
years = np.asarray(ref_mb_years)
ref_period = 'custom'
Expand Down
4 changes: 2 additions & 2 deletions oggm/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from oggm.core.climate import process_climate_data
from oggm.core.climate import process_custom_climate_data
from oggm.core.climate import historical_delta_method
from oggm.core.massbalance import mb_calibration_from_insitu_mb
from oggm.core.massbalance import mb_calibration_from_geodetic_mb
from oggm.core.massbalance import mb_calibration_from_wgms_mb
from oggm.core.massbalance import mb_calibration_from_scalar_mb
from oggm.core.massbalance import apparent_mb_from_linear_mb
from oggm.core.massbalance import apparent_mb_from_any_mb
from oggm.core.massbalance import fixed_geometry_mass_balance
Expand Down
8 changes: 4 additions & 4 deletions oggm/tests/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,9 @@ def init_hef(reset=False, border=40, logging_level='INFO', rgi_id=None,

ref_mb = mbdf.mean()
ref_period = f'{mbdf.index[0]}-01-01_{mbdf.index[-1] + 1}-01-01'
massbalance.mb_calibration_from_geodetic_mb(gdir,
ref_mb=ref_mb,
ref_period=ref_period)
massbalance.mb_calibration_from_scalar_mb(gdir,
ref_mb=ref_mb,
ref_period=ref_period)
massbalance.apparent_mb_from_any_mb(gdir, mb_years=(1953, 2002))
inversion.prepare_for_inversion(gdir)

Expand Down Expand Up @@ -557,7 +557,7 @@ def init_columbia_eb(dir_name, reset=False):
centerlines.fixed_dx_elevation_band_flowline(gdir)
centerlines.compute_downstream_line(gdir)
tasks.process_dummy_cru_file(gdir, seed=0)
tasks.mb_calibration_from_geodetic_mb(gdir)
tasks.mb_calibration_from_scalar_mb(gdir)
tasks.apparent_mb_from_any_mb(gdir)
return gdir

Expand Down
12 changes: 6 additions & 6 deletions oggm/tests/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_mb(self):
tmp_file=self.tf,
pre_file=self.pf)

execute_entity_task(tasks.mb_calibration_from_geodetic_mb, gdirs,
execute_entity_task(tasks.mb_calibration_from_scalar_mb, gdirs,
ref_period='2000-01-01_2010-01-01')

mbref = salem.GeoTiff(get_demo_file('mb_SouthGlacier.tif'))
Expand Down Expand Up @@ -181,7 +181,7 @@ def test_inversion_attributes(self):
execute_entity_task(tasks.process_cru_data, gdirs,
tmp_file=self.tf,
pre_file=self.pf)
execute_entity_task(tasks.mb_calibration_from_geodetic_mb, gdirs,
execute_entity_task(tasks.mb_calibration_from_scalar_mb, gdirs,
ref_period='2000-01-01_2010-01-01')

# Tested tasks
Expand Down Expand Up @@ -263,7 +263,7 @@ def test_inversion(self):
execute_entity_task(tasks.process_cru_data, gdirs,
tmp_file=self.tf,
pre_file=self.pf)
execute_entity_task(tasks.mb_calibration_from_geodetic_mb, gdirs,
execute_entity_task(tasks.mb_calibration_from_scalar_mb, gdirs,
ref_period='2000-01-01_2010-01-01')
execute_entity_task(tasks.apparent_mb_from_any_mb, gdirs,
mb_years=[2000, 2009])
Expand Down Expand Up @@ -340,7 +340,7 @@ def test_optimize_inversion(self):
execute_entity_task(tasks.process_cru_data, gdirs,
tmp_file=self.tf,
pre_file=self.pf)
execute_entity_task(tasks.mb_calibration_from_geodetic_mb, gdirs,
execute_entity_task(tasks.mb_calibration_from_scalar_mb, gdirs,
ref_period='2000-01-01_2010-01-01')
execute_entity_task(tasks.apparent_mb_from_any_mb, gdirs,
mb_years=[2000, 2009])
Expand Down Expand Up @@ -426,7 +426,7 @@ def test_workflow(self):
execute_entity_task(tasks.process_cru_data, gdirs,
tmp_file=self.tf,
pre_file=self.pf)
execute_entity_task(tasks.mb_calibration_from_geodetic_mb, gdirs,
execute_entity_task(tasks.mb_calibration_from_scalar_mb, gdirs,
ref_period='2000-01-01_2010-01-01')
execute_entity_task(tasks.apparent_mb_from_any_mb, gdirs,
mb_years=[2000, 2009])
Expand Down Expand Up @@ -562,7 +562,7 @@ def test_run(self):

# Climate tasks
tasks.process_dummy_cru_file(gdir, seed=0)
tasks.mb_calibration_from_geodetic_mb(gdir)
tasks.mb_calibration_from_scalar_mb(gdir)
tasks.apparent_mb_from_any_mb(gdir)

# Inversion tasks
Expand Down
4 changes: 2 additions & 2 deletions oggm/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_multiple_inversion():
gdirs = workflow.init_glacier_directories(hef_rgi)
workflow.gis_prepro_tasks(gdirs)
workflow.execute_entity_task(climate.process_climate_data, gdirs)
workflow.execute_entity_task(massbalance.mb_calibration_from_geodetic_mb,
workflow.execute_entity_task(massbalance.mb_calibration_from_scalar_mb,
gdirs, ref_mb_years=(1980, 2000), ref_mb=0)
workflow.execute_entity_task(massbalance.apparent_mb_from_any_mb,
gdirs, mb_years=(1980, 2000))
Expand Down Expand Up @@ -273,7 +273,7 @@ def test_multiple_models():
gdirs = workflow.init_glacier_directories(hef_rgi)
workflow.gis_prepro_tasks(gdirs)
workflow.execute_entity_task(climate.process_climate_data, gdirs)
workflow.execute_entity_task(massbalance.mb_calibration_from_geodetic_mb,
workflow.execute_entity_task(massbalance.mb_calibration_from_scalar_mb,
gdirs, ref_mb_years=(1980, 2000), ref_mb=0)
workflow.execute_entity_task(massbalance.apparent_mb_from_any_mb,
gdirs, mb_years=(1980, 2000))
Expand Down
Loading

0 comments on commit 1180d52

Please sign in to comment.