From 60331abb844718a5131068fa520622300a4b3e5c Mon Sep 17 00:00:00 2001 From: Robert Gieseke Date: Thu, 18 Jan 2018 10:39:00 +0100 Subject: [PATCH 1/2] Add test for parameters given as integers Division in Python2 is truncating (floor division). --- tests/test_fair.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/test_fair.py b/tests/test_fair.py index 6676df79..efcfcc88 100644 --- a/tests/test_fair.py +++ b/tests/test_fair.py @@ -31,9 +31,9 @@ def test_ten_GtC_pulse(): other_rf = np.zeros(emissions.size) for x in range(0,emissions.size): other_rf[x] = 0.5*np.sin(2*np.pi*(x)/14.0) - + C,F,T = fair.forward.fair_scm(emissions=emissions, other_rf=other_rf) - + datadir = os.path.join(os.path.dirname(__file__), 'ten_GtC_pulse/') C_expected = np.load(datadir + 'C.npy') F_expected = np.load(datadir + 'F.npy') @@ -104,3 +104,21 @@ def test_rcp85(): assert np.allclose(C, C_expected) assert np.allclose(F, F_expected) assert np.allclose(T, T_expected) + + +def test_division(): + # Ensure parameters given as integers are treated as floats when dividing + # (Python2 compatibility). + _, _, T = fair.forward.fair_scm( + emissions=fair.RCPs.rcp6.Emissions.emissions, + useMultigas=True, + d=np.array([239.0, 4.0]), + tcr_dbl=70.0 + ) + _, _, T_int_params = fair.forward.fair_scm( + emissions=fair.RCPs.rcp6.Emissions.emissions, + useMultigas=True, + d=np.array([239, 4]), + tcr_dbl=70 + ) + assert (T == T_int_params).all() From 0d6104c7aeb72aa556a59ed72657440d415822dd Mon Sep 17 00:00:00 2001 From: Robert Gieseke Date: Thu, 18 Jan 2018 10:45:58 +0100 Subject: [PATCH 2/2] Enable true division Closes #12 --- fair/forcing/aerosols.py | 2 ++ fair/forcing/bc_snow.py | 2 ++ fair/forcing/contrails.py | 2 ++ fair/forcing/ghg.py | 2 ++ fair/forcing/h2o_st.py | 2 ++ fair/forcing/ozone_st.py | 2 ++ fair/forward.py | 2 ++ 7 files changed, 14 insertions(+) diff --git a/fair/forcing/aerosols.py b/fair/forcing/aerosols.py index 2b9cb543..f44e39b7 100644 --- a/fair/forcing/aerosols.py +++ b/fair/forcing/aerosols.py @@ -1,3 +1,5 @@ +from __future__ import division + import numpy as np from ..constants import molwt diff --git a/fair/forcing/bc_snow.py b/fair/forcing/bc_snow.py index 1556957c..07d0342e 100644 --- a/fair/forcing/bc_snow.py +++ b/fair/forcing/bc_snow.py @@ -1,3 +1,5 @@ +from __future__ import division + def linear(emissions, E_ref=8.09, F_ref=0.04): E_BC = emissions[:,9] return E_BC * F_ref/E_ref diff --git a/fair/forcing/contrails.py b/fair/forcing/contrails.py index dde0179a..7c04935e 100644 --- a/fair/forcing/contrails.py +++ b/fair/forcing/contrails.py @@ -1,3 +1,5 @@ +from __future__ import division + import numpy as np from ..constants import molwt diff --git a/fair/forcing/ghg.py b/fair/forcing/ghg.py index 93f44c0c..20dd71fc 100644 --- a/fair/forcing/ghg.py +++ b/fair/forcing/ghg.py @@ -1,3 +1,5 @@ +from __future__ import division + import numpy as np def etminan(C, Cpi, F2x=3.74): diff --git a/fair/forcing/h2o_st.py b/fair/forcing/h2o_st.py index bd19295a..73272d4f 100644 --- a/fair/forcing/h2o_st.py +++ b/fair/forcing/h2o_st.py @@ -1,3 +1,5 @@ +from __future__ import division + import numpy as np def linear(F_CH4, ratio=0.15): diff --git a/fair/forcing/ozone_st.py b/fair/forcing/ozone_st.py index 2f1d8d40..9d6b8191 100644 --- a/fair/forcing/ozone_st.py +++ b/fair/forcing/ozone_st.py @@ -1,3 +1,5 @@ +from __future__ import division + import numpy as np from ..constants import cl_atoms, br_atoms, fracrel diff --git a/fair/forward.py b/fair/forward.py index 756e00be..475163c1 100644 --- a/fair/forward.py +++ b/fair/forward.py @@ -1,3 +1,5 @@ +from __future__ import division + import inspect import numpy as np from scipy.optimize import root