def test_temp_bias(): """Test temp bias""" mb = MassBalance(ela=2000, gradient=10) mb.temp_bias = 1 assert mb.temp_bias == 1 assert mb.ela_h == mb.orig_ela_h + 150
def test_temp_bias_setter(): """Test the setter of the temp_bias_series.""" mb = MassBalance(ela=2000, gradient=10) # Check year dtype data = ["1", "hello", "2"] with pytest.raises(Exception) as e_info: mb.temp_bias_series = data # Finally something that should work. data = [1, 1.5, 1] mb.temp_bias_series = data assert len(mb.temp_bias_series.year) == 4 assert_equal(mb.temp_bias_series.bias, np.array([0, 1, 1.5, 1]))
def test_constructor(): mb = MassBalance(ela=2000, gradient=[10, 5], breakpoints=[2000]) # Does assignment work? assert mb.grad == [10, 5] mb = MassBalance(ela=2000, gradient=10) assert mb.grad == 10 assert mb.ela_h == 2000 assert mb.orig_ela_h == 2000 assert mb.temp_bias == 0 # We are not allowed to pass negative gradients. with pytest.raises(Exception) as e_info: mb = MassBalance(ela=2000, gradient=[10, -5], breakpoints=[2000])
def test_progress_to_equilibrium(): """Test progressing a collection of glaciers to equilibrium.""" mb = MassBalance(ela=3000, gradient=8) bed = GlacierBed(top=3700, bottom=1500, width=600) glacier1 = Glacier(bed=bed, mass_balance=mb) glacier2 = Glacier(bed=bed, mass_balance=mb) collection = GlacierCollection([glacier1, glacier2]) # Since the glaciers in the collection have the same attributes, they should # reach the same equilibrium state. collection.progress_to_equilibrium() assert collection.glaciers[0].age == collection.glaciers[1].age
def test_get_annual_mb(): """Test the annual mb computation""" # Non-linear mass balance mb = MassBalance(ela=2000, gradient=[10, 5], breakpoints=[2000]) heights = [3000, 2500, 2000, 1500, 1000] computed_mbs = mb.get_annual_mb(heights) corr_mbs = ( np.array([1000 * 10, 500 * 10, 0, -500 * 5, -1000 * 5]) / SEC_IN_YEAR / mb.rho ) assert_equal(computed_mbs, corr_mbs) # Another non-linear mb. # Breakpoints and heights does not have to match for it to work. mb = MassBalance(ela=2000, gradient=[10, 5, 3], breakpoints=[2000, 1200]) computed_mbs = mb.get_annual_mb(heights) # INtersection m = (1200 - 2000) * (5 - 3) corr_mbs = ( np.array([1000 * 10, 500 * 10, 0, -500 * 5, -1000 * 3 + m]) / SEC_IN_YEAR / mb.rho ) assert_equal(computed_mbs, corr_mbs) # Liner mass balance. mb = MassBalance(ela=2000, gradient=10) corr_mbs = (np.asarray(heights) - 2000) * 10 / SEC_IN_YEAR / mb.rho computed_mbs = mb.get_annual_mb(heights) assert_equal(corr_mbs, computed_mbs)
def test_progress_to_year(): """Test progressing the collection to a specified year""" mb = MassBalance(ela=3000, gradient=8) bed = GlacierBed(top=3700, bottom=1500, width=600) glacier1 = Glacier(bed=bed, mass_balance=mb) glacier2 = Glacier(bed=bed, mass_balance=mb) collection = GlacierCollection([glacier1, glacier2]) collection = GlacierCollection([glacier1, glacier2]) # Essntially all glaciers in the collection should be of the same age. year = 100 collection.progress_to_year(year) # Check the ages. assert collection.glaciers[0].age == year assert collection.glaciers[1].age == year # Empty collection should raise. collection = GlacierCollection() with pytest.raises(Exception) as e_info: collection.progress_to_year(year)
def test_gradient_lookup(): """Test the gradient lookup function""" # Fancy case mb = MassBalance(ela=2000, gradient=[10, 5], breakpoints=[2000]) # Computed gradients heights = [3000, 2500, 2000, 1500, 1000] computed_grads = mb._gradient_lookup(heights) # Correct grads corr_grads = [10, 10, 5, 5, 5] assert_equal(computed_grads, corr_grads) # Simple case mb = MassBalance(ela=2000, gradient=10) # Computed gradients computed_grads = mb._gradient_lookup(heights) # Correct grads corr_grads = [10, 10, 10, 10, 10] assert_equal(computed_grads, corr_grads)
from oggm_edu import Glacier, SurgingGlacier, GlacierBed, MassBalance from numpy.testing import assert_equal import pytest real_mb = MassBalance(ela=3000, gradient=5) real_bed = GlacierBed(top=3400, bottom=1500, width=500) def test_glacier_constructor(): """The glacier consructor is fairly simple, we only check that exceptions are raised.""" with pytest.raises(Exception) as e_info: fake_bed = ["hello"] Glacier(bed=fake_bed, mass_balance=real_mb) with pytest.raises(Exception) as e_info: fake_mb = 21654 Glacier(bed=real_bed, mass_balance=fake_mb) # Initial glacier height should match bed height. glacier = Glacier(bed=real_bed, mass_balance=real_mb) assert_equal(glacier.surface_h, real_bed.bed_h) def test_copy(): """Is copy really producing a copy?""" glacier = Glacier(bed=real_bed, mass_balance=real_mb) glacier_copy = glacier.copy() assert glacier != glacier_copy assert isinstance(glacier_copy, Glacier)
from oggm_edu import GlacierBed, Glacier, MassBalance, GlacierCollection import pytest mb = MassBalance(ela=3000, gradient=8) bed = GlacierBed(top=3700, bottom=1500, width=600) glacier1 = Glacier(bed=bed, mass_balance=mb) glacier2 = Glacier(bed=bed, mass_balance=mb) bed_new = GlacierBed(top=3700, bottom=2500, width=300, slopes=45) glacier3 = Glacier(bed=bed_new, mass_balance=mb) # We need a global collection for testing the plots, for some efficiency. collection = GlacierCollection() collection.fill(glacier1, 2) collection.change_attributes(attributes_to_change={"gradient": [10, 15]}) year = 200 collection.progress_to_year(year) def test_constructor(): """Can we initialise a collection from a list directly?""" collection = GlacierCollection([glacier1, glacier2]) # Should have length 2. assert len(collection.glaciers) == 2 def test_check_collection(): """Check if the glaciers in the collection are the same.""" # Check collection should be true since glaciers have the same beds. collection = GlacierCollection([glacier1, glacier2])