Skip to content

Commit

Permalink
Updating to NumPy > 2.0 (#1742)
Browse files Browse the repository at this point in the history
* fix tests and warnings for numpy>2.0

* fix further warnings

* pep8
  • Loading branch information
pat-schmitt authored Sep 16, 2024
1 parent 865475d commit 5d87481
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
10 changes: 4 additions & 6 deletions oggm/core/dynamic_spinup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,8 @@ def dynamic_melt_f_run(
with utils.DisableLogger():
ds = utils.compile_run_output(gdir, input_filesuffix=output_filesuffix,
path=False)
dmdtda_mdl = ((ds.volume.loc[yr1_ref_mb].values -
ds.volume.loc[yr0_ref_mb].values) /
dmdtda_mdl = ((ds.volume.loc[yr1_ref_mb].values[0] -
ds.volume.loc[yr0_ref_mb].values[0]) /
gdir.rgi_area_m2 /
(yr1_ref_mb - yr0_ref_mb) *
cfg.PARAMS['ice_density'])
Expand Down Expand Up @@ -1960,10 +1960,8 @@ def cost_fct(melt_f, model_dynamic_spinup_end):
model_dynamic_spinup_end.append(copy.deepcopy(model_dynamic_spinup))

# calculate the mismatch of dmdtda
try:
cost = float(dmdtda_mdl - ref_dmdtda)
except:
t = 1
cost = float(dmdtda_mdl - ref_dmdtda)

return cost

def init_cost_fun():
Expand Down
2 changes: 1 addition & 1 deletion oggm/core/flowline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ def run_until_and_store(self, y1,

# First deal with spinup (we compute volume change only)
if do_fixed_spinup:
spinup_vol = monthly_time * 0
spinup_vol = monthly_time * 0.
for fl_id, fl in enumerate(self.fls):

h = fl.surface_h
Expand Down
10 changes: 6 additions & 4 deletions oggm/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3858,9 +3858,9 @@ def reset_melt_f():

df_ref_dmdtda = utils.get_geodetic_mb_dataframe().loc[gdir.rgi_id]
sel = df_ref_dmdtda.loc[df_ref_dmdtda['period'] == ref_period]
ref_dmdtda = float(sel['dmdtda'])
ref_dmdtda = float(sel['dmdtda'].iloc[0])
ref_dmdtda *= 1000 # kg m-2 yr-1
err_ref_dmdtda = float(sel['err_dmdtda'])
err_ref_dmdtda = float(sel['err_dmdtda'].iloc[0])
err_ref_dmdtda *= 1000 # kg m-2 yr-1

melt_f_max = 1000 * 12 / 365
Expand Down Expand Up @@ -5713,8 +5713,10 @@ def test_distribute(self, hef_elev_gdir, inversion_params):
fl_diag = fl_diag.isel(dis_along_flowline=slice(0, band_ids.max()+1))
for bid in band_ids:
thick_band = thick.where(ds.band_index == bid)
fl_diag['volume_m3_dis'].data[:, bid] = thick_band.sum(dim=['x', 'y']) * dx2
fl_diag['area_m2_dis'].data[:, bid] = (thick_band > 1).sum(dim=['x', 'y']) * dx2
fl_diag['volume_m3_dis'].data[:, bid] = thick_band.sum(dim=['x', 'y']
).values * dx2
fl_diag['area_m2_dis'].data[:, bid] = (thick_band > 1).sum(dim=['x', 'y']
).values * dx2

for yr in [2003]:
# All the other years have larger errors but they somehow still look
Expand Down
16 changes: 13 additions & 3 deletions oggm/utils/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ def floatyear_to_date(yr):
The floating year
"""

if isinstance(yr, xr.DataArray):
yr = yr.values

out_y, remainder = np.divmod(yr, 1)
out_y = out_y.astype(int)

Expand All @@ -700,9 +703,6 @@ def floatyear_to_date(yr):
if (isinstance(yr, list) or isinstance(yr, np.ndarray)) and len(yr) == 1:
out_y = out_y.item()
out_m = out_m.item()
elif isinstance(yr, xr.DataArray):
out_y = np.array(out_y)
out_m = np.array(out_m)

return out_y, out_m

Expand Down Expand Up @@ -769,6 +769,11 @@ def calendardate_to_hydrodate(y, m, start_month=None):
the first month of the hydrological year
"""

if isinstance(y, xr.DataArray):
y = y.values
if isinstance(m, xr.DataArray):
m = m.values

if start_month is None:
raise InvalidParamsError('In order to avoid confusion, we now force '
'callers of this function to specify the '
Expand All @@ -793,6 +798,11 @@ def monthly_timeseries(y0, y1=None, ny=None, include_last_year=False):
----------
"""

if isinstance(y0, xr.DataArray):
y0 = y0.values
if isinstance(y1, xr.DataArray):
y1 = y1.values

if y1 is not None:
years = np.arange(np.floor(y0), np.floor(y1) + 1)
elif ny is not None:
Expand Down
10 changes: 8 additions & 2 deletions oggm/utils/_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2313,9 +2313,9 @@ def extend_past_climate_run(past_run_file=None,

# Time
ods['hydro_year'].data[:] = years
ods['hydro_month'].data[:] = ods['hydro_month'][-1]
ods['hydro_month'].data[:] = ods['hydro_month'][-1].item()
ods['calendar_year'].data[:] = years
ods['calendar_month'].data[:] = ods['calendar_month'][-1]
ods['calendar_month'].data[:] = ods['calendar_month'][-1].item()
for vn in ['hydro_year', 'hydro_month', 'calendar_year', 'calendar_month']:
ods[vn] = ods[vn].astype(int)

Expand Down Expand Up @@ -3423,6 +3423,12 @@ def write_monthly_climate_file(self, time, prcp, temp,
Apply a suffix to the file
"""

if isinstance(prcp, xr.DataArray):
prcp = prcp.values
if isinstance(temp, xr.DataArray):
temp = temp.values
if isinstance(temp_std, xr.DataArray):
temp_std = temp_std.values
# overwrite as default
fpath = self.get_filepath(file_name, filesuffix=filesuffix)
if os.path.exists(fpath):
Expand Down
2 changes: 1 addition & 1 deletion oggm/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ def merge_gridded_data(gdirs, output_folder=None,
if slice_of_var is not None:
if dim in slice_of_var:
coord_val = ds_templ[coord].sel(
{dim: slice_of_var[dim]})
{dim: slice_of_var[dim]}).values
else:
coord_val = ds_templ[coord].values
else:
Expand Down

0 comments on commit 5d87481

Please sign in to comment.