Skip to content

Commit

Permalink
Merge 87a699c into 0a25564
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion committed Oct 22, 2018
2 parents 0a25564 + 87a699c commit f30c9d1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 42 deletions.
4 changes: 2 additions & 2 deletions benchmarks/track_model_results.py
Expand Up @@ -85,13 +85,13 @@ def track_rectangular_ratio(self, gdir):

def track_mustar(self, gdir):
self.cfg_init()
df = pd.read_csv(gdir.get_filepath('local_mustar')).iloc[0]
df = gdir.read_json('local_mustar')
assert df['mu_star_allsame']
return df['mu_star_glacierwide']

def track_bias(self, gdir):
self.cfg_init()
df = pd.read_csv(gdir.get_filepath('local_mustar')).iloc[0]
df = gdir.read_json('local_mustar')
return df['bias']

def track_mb_1980_avg(self, gdir):
Expand Down
4 changes: 2 additions & 2 deletions oggm/cfg.py
Expand Up @@ -171,8 +171,8 @@ def __setitem__(self, key, value):
_doc = 'The monthly GCM climate timeseries stored in a netCDF file.'
BASENAMES['cesm_data'] = ('cesm_data.nc', _doc)

_doc = "A csv containing the glacier's t*, bias, and the flowlines' mu*"
BASENAMES['local_mustar'] = ('local_mustar.csv', _doc)
_doc = "A dict containing the glacier's t*, bias, and the flowlines' mu*"
BASENAMES['local_mustar'] = ('local_mustar.json', _doc)

_doc = 'List of dicts containing the data needed for the inversion.'
BASENAMES['inversion_input'] = ('inversion_input.pkl', _doc)
Expand Down
29 changes: 14 additions & 15 deletions oggm/core/climate.py
Expand Up @@ -952,8 +952,7 @@ def calving_mb(gdir):


@entity_task(log, writes=['local_mustar'])
def local_t_star(gdir, *, ref_df=None,
tstar=None, bias=None):
def local_t_star(gdir, *, ref_df=None, tstar=None, bias=None):
"""Compute the local t* and associated glacier-wide mu*.
If ``tstar`` and ``bias`` are not provided, they will be interpolated from
Expand Down Expand Up @@ -1054,13 +1053,13 @@ def local_t_star(gdir, *, ref_df=None,
if not (cfg.PARAMS['min_mu_star'] < mustar < cfg.PARAMS['max_mu_star']):
raise MassBalanceCalibrationError('mu* out of specified bounds.')

# Scalars in a small dataframe for later
df = pd.DataFrame()
df['rgi_id'] = [gdir.rgi_id]
df['t_star'] = [tstar]
df['bias'] = [bias]
df['mu_star_glacierwide'] = [mustar]
df.to_csv(gdir.get_filepath('local_mustar'), index=False)
# Scalars in a small dict for later
df = dict()
df['rgi_id'] = gdir.rgi_id
df['t_star'] = int(tstar)
df['bias'] = bias
df['mu_star_glacierwide'] = mustar
gdir.write_json(df, 'local_mustar')


def _mu_star_per_minimization(x, fls, cmb, temp, prcp, widths):
Expand Down Expand Up @@ -1168,9 +1167,9 @@ def mu_star_calibration(gdir):
"""

# Interpolated data
df = pd.read_csv(gdir.get_filepath('local_mustar'))
t_star = df['t_star'].iloc[0]
bias = df['bias'].iloc[0]
df = gdir.read_json('local_mustar')
t_star = df['t_star']
bias = df['bias']

# For each flowline compute the apparent MB
fls = gdir.read_pickle('inversion_flowlines')
Expand Down Expand Up @@ -1220,10 +1219,10 @@ def mu_star_calibration(gdir):
# Store diagnostics
mus = []
weights = []
for i, fl in enumerate(fls):
df['mustar_flowline_{:03d}'.format(i+1)] = fl.mu_star
for fl in fls:
mus.append(fl.mu_star)
weights.append(np.sum(fl.widths))
df['mu_star_per_flowline'] = mus
df['mu_star_flowline_avg'] = np.average(mus, weights=weights)
all_same = np.allclose(mus, mus[0], atol=1e-3)
df['mu_star_allsame'] = all_same
Expand All @@ -1235,7 +1234,7 @@ def mu_star_calibration(gdir):
'glacier wide mu* and the '
'flowlines mu*.')
# Write
df.to_csv(gdir.get_filepath('local_mustar'), index=False)
gdir.write_json(df, 'local_mustar')


@entity_task(log, writes=['inversion_flowlines', 'linear_mb_params'])
Expand Down
18 changes: 9 additions & 9 deletions oggm/core/massbalance.py
Expand Up @@ -262,10 +262,10 @@ def __init__(self, gdir, mu_star=None, bias=None,
super(PastMassBalance, self).__init__()
self.valid_bounds = [-1e4, 2e4] # in m
if mu_star is None:
df = pd.read_csv(gdir.get_filepath('local_mustar'))
mu_star = df['mu_star_glacierwide'][0]
df = gdir.read_json('local_mustar')
mu_star = df['mu_star_glacierwide']
if check_calib_params:
if not df['mu_star_allsame'][0]:
if not df['mu_star_allsame']:
raise RuntimeError('You seem to use the glacier-wide mu* '
'to compute the mass-balance although '
'this glacier has different mu* for '
Expand All @@ -275,8 +275,8 @@ def __init__(self, gdir, mu_star=None, bias=None,

if bias is None:
if cfg.PARAMS['use_bias_for_run']:
df = pd.read_csv(gdir.get_filepath('local_mustar'))
bias = df['bias'][0]
df = gdir.read_json('local_mustar')
bias = df['bias']
else:
bias = 0.

Expand Down Expand Up @@ -478,8 +478,8 @@ def __init__(self, gdir, mu_star=None, bias=None,
input_filesuffix=input_filesuffix)

if y0 is None:
df = pd.read_csv(gdir.get_filepath('local_mustar'))
y0 = df['t_star'][0]
df = gdir.read_json('local_mustar')
y0 = df['t_star']

# This is a quick'n dirty optimisation
try:
Expand Down Expand Up @@ -658,8 +658,8 @@ def __init__(self, gdir, mu_star=None, bias=None,
self.years = self.mbmod.years
else:
if y0 is None:
df = pd.read_csv(gdir.get_filepath('local_mustar'))
y0 = df['t_star'][0]
df = gdir.read_json('local_mustar')
y0 = df['t_star']
self.years = np.arange(y0-halfsize, y0+halfsize+1)
self.yr_range = (self.years[0], self.years[-1]+1)
self.ny = len(self.years)
Expand Down
10 changes: 5 additions & 5 deletions oggm/tests/test_models.py
Expand Up @@ -292,9 +292,9 @@ def test_past_mb_model(self):
gdir = self.gdir
init_present_time_glacier(gdir)

df = pd.read_csv(gdir.get_filepath('local_mustar'))
mu_star = df['mu_star_glacierwide'][0]
bias = df['bias'][0]
df = gdir.read_json('local_mustar')
mu_star = df['mu_star_glacierwide']
bias = df['bias']

# Climate period
yrp = [1851, 2000]
Expand Down Expand Up @@ -488,8 +488,8 @@ def test_constant_mb_model(self):
gdir = self.gdir
init_present_time_glacier(gdir)

df = pd.read_csv(gdir.get_filepath('local_mustar'))
bias = df['bias'][0]
df = gdir.read_json('local_mustar')
bias = df['bias']

h, w = gdir.get_inversion_flowline_hw()

Expand Down
11 changes: 5 additions & 6 deletions oggm/tests/test_prepro.py
Expand Up @@ -1272,7 +1272,7 @@ def test_find_tstars(self):
climate.local_t_star(gdir)
cfg.PARAMS['tstar_search_window'] = [0, 0]

df = pd.read_csv(gdir.get_filepath('local_mustar'))
df = gdir.read_json('local_mustar')
np.testing.assert_allclose(df['t_star'], t_star)
np.testing.assert_allclose(df['bias'], bias)

Expand Down Expand Up @@ -1363,7 +1363,7 @@ def test_local_t_star(self):
np.testing.assert_allclose(tmb, 0., atol=0.01)
np.testing.assert_allclose(fls[-1].flux[-1], 0., atol=0.01)

df = pd.read_csv(gdir.get_filepath('local_mustar')).iloc[0]
df = gdir.read_json('local_mustar')
assert df['mu_star_allsame']
np.testing.assert_allclose(mu_ref, df['mu_star_flowline_avg'],
atol=1e-3)
Expand Down Expand Up @@ -1522,13 +1522,13 @@ def test_correct(self):
mus = np.array([fl.mu_star for fl in fls])
assert np.max(mus[[1, 2, 3]]) < (np.max(mus[[0, -1]]) / 2)

df = pd.read_csv(gdir.get_filepath('local_mustar'))
mu_star_gw = df['mu_star_glacierwide'][0]
df = gdir.read_json('local_mustar')
mu_star_gw = df['mu_star_glacierwide']

assert np.max(mus[[1, 2, 3]]) < mu_star_gw
assert np.min(mus[[0, -1]]) > mu_star_gw

bias = df['bias'][0]
bias = df['bias']
np.testing.assert_allclose(bias, 0)

from oggm.core.massbalance import (MultipleFlowlineMassBalance,
Expand Down Expand Up @@ -1573,7 +1573,6 @@ def test_and_compare_two_methods(self):
fls = gdir.read_pickle('inversion_flowlines')

# These are the params:
# pd.read_csv(gdir.get_filepath('local_mustar'))
# rgi_id RGI50-11.00666
# t_star 1931
# bias 0
Expand Down
41 changes: 38 additions & 3 deletions oggm/utils.py
Expand Up @@ -2566,7 +2566,7 @@ def glacier_characteristics(gdirs, filesuffix='', path=True,
pass
try:
# MB calib
df = pd.read_csv(gdir.get_filepath('local_mustar')).iloc[0]
df = gdir.read_json('local_mustar')
d['t_star'] = df['t_star']
d['mu_star_glacierwide'] = df['mu_star_glacierwide']
d['mu_star_flowline_avg'] = df['mu_star_flowline_avg']
Expand Down Expand Up @@ -3127,8 +3127,7 @@ def read_pickle(self, filename, use_compression=None, filesuffix=''):

return out

def write_pickle(self, var, filename, use_compression=None,
filesuffix=''):
def write_pickle(self, var, filename, use_compression=None, filesuffix=''):
""" Writes a variable to a pickle on disk.
Parameters
Expand All @@ -3150,6 +3149,42 @@ def write_pickle(self, var, filename, use_compression=None,
with _open(fp, 'wb') as f:
pickle.dump(var, f, protocol=-1)

def read_json(self, filename, filesuffix=''):
"""Reads a JSON file located in the directory.
Parameters
----------
filename : str
file name (must be listed in cfg.BASENAME)
filesuffix : str
append a suffix to the filename (useful for experiments).
Returns
-------
A dictionary read from the JSON file
"""

fp = self.get_filepath(filename, filesuffix=filesuffix)
with open(fp, 'r') as f:
out = json.load(f)
return out

def write_json(self, var, filename, filesuffix=''):
""" Writes a variable to a pickle on disk.
Parameters
----------
var : object
the variable to write to JSON (must be a dictionary)
filename : str
file name (must be listed in cfg.BASENAME)
filesuffix : str
append a suffix to the filename (useful for experiments).
"""
fp = self.get_filepath(filename, filesuffix=filesuffix)
with open(fp, 'w') as f:
json.dump(var, f)

def create_gridded_ncdf_file(self, fname):
"""Makes a gridded netcdf file template.
Expand Down

0 comments on commit f30c9d1

Please sign in to comment.