Skip to content

Commit

Permalink
Add a way to initialize OGGM without accessing the demo files (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion authored Jan 3, 2020
1 parent eb65982 commit 8c020b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
44 changes: 30 additions & 14 deletions oggm/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,10 @@ def workflow(self, message, *args, **kws):
level=getattr(logging, logging_level))


def initialize(file=None, logging_level='INFO'):
"""Read the configuration file containing the run's parameters.
def initialize_minimal(file=None, logging_level='INFO'):
"""Same as initialise() but without requiring any download of data.
This should be the first call, before using any of the other OGGM modules
for most (all?) OGGM simulations.
This is useful for "flowline only" OGGM applications
Parameters
----------
Expand All @@ -302,11 +301,9 @@ def initialize(file=None, logging_level='INFO'):
logging_level : str
set a logging level. See :func:`set_logging_config` for options.
"""

global IS_INITIALIZED
global PARAMS
global PATHS
global DATA

set_logging_config(logging_level=logging_level)

Expand Down Expand Up @@ -376,10 +373,6 @@ def initialize(file=None, logging_level='INFO'):
k = 'use_shape_factor_for_fluxbasedmodel'
PARAMS[k] = cp[k]

# Make sure we have a proper cache dir
from oggm.utils import download_oggm_files, get_demo_file
download_oggm_files()

# Delete non-floats
ltr = ['working_dir', 'dem_file', 'climate_file', 'use_tar_shapefiles',
'grid_dx_method', 'run_mb_calibration', 'compress_climate_netcdf',
Expand All @@ -401,6 +394,33 @@ def initialize(file=None, logging_level='INFO'):
for k in cp:
PARAMS[k] = cp.as_float(k)

# Empty defaults
set_intersects_db()
IS_INITIALIZED = True


def initialize(file=None, logging_level='INFO'):
"""Read the configuration file containing the run's parameters.
This should be the first call, before using any of the other OGGM modules
for most (all?) OGGM simulations.
Parameters
----------
file : str
path to the configuration file (default: OGGM params.cfg)
logging_level : str
set a logging level. See :func:`set_logging_config` for options.
"""
global PARAMS
global DATA

initialize_minimal(file=file, logging_level=logging_level)

# Make sure we have a proper cache dir
from oggm.utils import download_oggm_files, get_demo_file
download_oggm_files()

# Read-in the reference t* data for all available models types (oggm, vas)
model_prefixes = ['oggm_', 'vas_']
for prefix in model_prefixes:
Expand All @@ -414,10 +434,6 @@ def initialize(file=None, logging_level='INFO'):
mbpar = json.load(fp)
PARAMS[prefix + fn + '_calib_params'] = mbpar

# Empty defaults
set_intersects_db()
IS_INITIALIZED = True

# Pre extract cru cl to avoid problems by multiproc
from oggm.utils import get_cru_cl_file
get_cru_cl_file()
Expand Down
6 changes: 6 additions & 0 deletions oggm/tests/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ def patch_url_retrieve_github(url, *args, **kwargs):
return oggm_urlretrieve(url, *args, **kwargs)


def patch_minimal_download_oggm_files(*args, **kwargs):
"""A simple patch to make sure we don't download."""

raise RuntimeError('We should not be there in minimal mode')


def use_multiprocessing():
try:
return strtobool(os.getenv("OGGM_TEST_MULTIPROC", "True"))
Expand Down
20 changes: 16 additions & 4 deletions oggm/tests/test_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,38 @@

# Local imports
from oggm.core.massbalance import LinearMassBalance
from oggm import cfg
from oggm import cfg, utils
from oggm.utils import rmsd
from oggm.cfg import SEC_IN_DAY
from oggm.core.sia2d import Upstream2D

# Tests
from oggm.tests.funcs import dummy_constant_bed
from oggm.tests.funcs import (dummy_constant_bed,
patch_minimal_download_oggm_files)
from oggm.core.flowline import KarthausModel, FluxBasedModel
from oggm.tests.ext.sia_fluxlim import MUSCLSuperBeeModel

FluxBasedModel = partial(FluxBasedModel, inplace=True)
KarthausModel = partial(KarthausModel, inplace=True)
MUSCLSuperBeeModel = partial(MUSCLSuperBeeModel, inplace=True)

_patched_download_oggm_files = None


def setup_module(module):
module._patched_download_oggm_files = utils.download_oggm_files
utils._downloads.download_oggm_files = patch_minimal_download_oggm_files


def teardown_module(module):
utils._downloads.download_oggm_files = module._patched_download_oggm_files


class TestIdealisedCases(unittest.TestCase):

def setUp(self):
N = 3
cfg.initialize()
cfg.initialize_minimal()
self.glen_a = 2.4e-24 # Modern style Glen parameter A
self.aglen_old = (N + 2) * 1.9e-24 / 2. # outdated value
self.fd = 2. * self.glen_a / (N + 2.) # equivalent to glen_a
Expand Down Expand Up @@ -90,7 +102,7 @@ def test_run_until_and_store(self):
class TestSia2d(unittest.TestCase):

def setUp(self):
cfg.initialize()
cfg.initialize_minimal()

def tearDown(self):
pass
Expand Down
1 change: 1 addition & 0 deletions oggm/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
pytest.importorskip('rasterio')
pytest.importorskip('salem')


def setup_module(module):
module._url_retrieve = utils.oggm_urlretrieve
oggm.utils._downloads.oggm_urlretrieve = patch_url_retrieve_github
Expand Down

0 comments on commit 8c020b3

Please sign in to comment.