Skip to content

Commit

Permalink
Fixed issue #1: Add "model_version" parameter to cube configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
forman committed Nov 5, 2015
1 parent a324dcc commit a825d6e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/cablab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pkg_resources import iter_entry_points

from cablab.cube import BaseCubeSourceProvider
from cablab.cube import CUBE_MODEL_VERSION
from cablab.cube import Cube
from cablab.cube import CubeConfig
from cablab.cube import CubeData
Expand All @@ -43,10 +44,11 @@ def _load_source_providers():
SOURCE_PROVIDERS = _load_source_providers()

__all__ = [
'BaseCubeSourceProvider',
'CUBE_MODEL_VERSION',
'Cube',
'CubeConfig',
'CubeData',
'CubeSourceProvider',
'BaseCubeSourceProvider',
'SOURCE_PROVIDERS',
]
24 changes: 20 additions & 4 deletions src/cablab/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import netCDF4

import cablab
import cablab.util

# The current version of the data cube's configuration and data model.
CUBE_MODEL_VERSION = '0.1'


class CubeSourceProvider(metaclass=ABCMeta):
Expand Down Expand Up @@ -225,7 +229,9 @@ def __init__(self,
end_time=datetime(2011, 1, 1),
variables=None,
file_format='NETCDF4_CLASSIC',
compression=False):
compression=False,
model_version=CUBE_MODEL_VERSION):
self.model_version = model_version
self.spatial_res = spatial_res
self.grid_x0 = grid_x0
self.grid_y0 = grid_y0
Expand Down Expand Up @@ -291,11 +297,21 @@ def load(path):
:param path: The file's path name.
:return: A new CubeConfig instance
"""
kwargs = dict()
config = dict()
with open(path) as fp:
code = fp.read()
exec(code, {'datetime': __import__('datetime')}, kwargs)
return CubeConfig(**kwargs)
exec(code, {'datetime': __import__('datetime')}, config)

CubeConfig._ensure_compatible_config(config)

return CubeConfig(**config)

@staticmethod
def _ensure_compatible_config(config_dict):
model_version = config_dict.get('model_version', None)
# Here: check for compatibility with Cube.MODEL_VERSION, convert if possible, otherwise raise error.
if model_version is None or model_version < CUBE_MODEL_VERSION:
print('WARNING: outdated cube model version, current version is %s' % CUBE_MODEL_VERSION)

def store(self, path):
"""
Expand Down
24 changes: 15 additions & 9 deletions test/test_cube.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from unittest import TestCase
import os
import shutil
from datetime import datetime
import os
from unittest import TestCase

import numpy

from cablab import CubeSourceProvider, CubeConfig, Cube
from cablab import CubeSourceProvider, CubeConfig, Cube, CUBE_MODEL_VERSION

CUBE_DIR = 'testcube'

Expand All @@ -21,8 +21,13 @@ def test_validate(self):
with self.assertRaises(ValueError):
CubeConfig(grid_y0=-1)

def test_model_version_is_current(self):
config = CubeConfig()
self.assertEqual(CUBE_MODEL_VERSION, config.model_version)

def test_properties(self):
config = CubeConfig()

self.assertEqual(-180, config.easting)
self.assertEqual(90, config.northing)
self.assertEqual(((-180, -90), (180, 90)), config.geo_bounds)
Expand Down Expand Up @@ -71,13 +76,14 @@ def test_update(self):
self.assertEqual(cube.config.file_format, cube2.config.file_format)
self.assertEqual(cube.config.compression, cube2.config.compression)

provider = CubeSourceProviderMock(cube2.config, start_time=datetime(2006, 12, 15), end_time=datetime(2007, 1, 15))
provider = CubeSourceProviderMock(cube2.config, start_time=datetime(2006, 12, 15),
end_time=datetime(2007, 1, 15))
cube2.update(provider)
self.assertEqual([(datetime(2006, 12, 11, 0, 0), datetime(2006, 12, 19, 0, 0)), # 8 days
(datetime(2006, 12, 19, 0, 0), datetime(2006, 12, 27, 0, 0)), # 8 days
(datetime(2006, 12, 27, 0, 0), datetime(2007, 1, 1, 0, 0)), # 8 days
(datetime(2007, 1, 1, 0, 0), datetime(2007, 1, 9, 0, 0)), # 8 days
(datetime(2007, 1, 9, 0, 0), datetime(2007, 1, 17, 0, 0))], # 8 days
self.assertEqual([(datetime(2006, 12, 11, 0, 0), datetime(2006, 12, 19, 0, 0)), # 8 days
(datetime(2006, 12, 19, 0, 0), datetime(2006, 12, 27, 0, 0)), # 8 days
(datetime(2006, 12, 27, 0, 0), datetime(2007, 1, 1, 0, 0)), # 8 days
(datetime(2007, 1, 1, 0, 0), datetime(2007, 1, 9, 0, 0)), # 8 days
(datetime(2007, 1, 9, 0, 0), datetime(2007, 1, 17, 0, 0))], # 8 days
provider.trace)

self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2006_LAI.nc"))
Expand Down

0 comments on commit a825d6e

Please sign in to comment.