Skip to content

Commit

Permalink
Merge pull request #506 from MetOffice/fix-tutorial-um-database
Browse files Browse the repository at this point in the history
Fix UM database tutorial example
  • Loading branch information
andrewgryan authored Jul 4, 2022
2 parents e171a4b + eadba24 commit 8acd8b4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
23 changes: 22 additions & 1 deletion forest/drivers/unified_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ def full_path(self, name):
return os.path.join(self.directory, name)


class Facade:
"""Translation layer between dataset labels and file system patterns
"""

def __init__(self, pattern, database):
self.pattern = pattern
self.database = database

def variables(self, _label):
return self.database.variables(self.pattern)

def initial_times(self, _label, variable):
return self.database.initial_times(self.pattern, variable)

def valid_times(self, _label, variable, initial_time):
return self.database.valid_times(self.pattern, variable, initial_time)

def pressures(self, _label, variable, initial_time):
return self.database.pressures(self.pattern, variable, initial_time)


class Dataset:
def __init__(
self,
Expand All @@ -114,7 +135,7 @@ def __init__(

def navigator(self):
if self.use_database:
return self.database
return Facade(self.pattern, self.database)
else:
return Navigator(self.pattern)

Expand Down
12 changes: 7 additions & 5 deletions forest/tutorial/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def build_um_config(build_dir):
pattern: "*{}"
directory: {}
locator: database
database_path: database.db
database_path: {}
""".format(
UM_FILE, build_dir
UM_FILE, build_dir, db_path(build_dir)
)
print("writing: {}".format(path))
with open(path, "w") as stream:
Expand Down Expand Up @@ -181,13 +181,15 @@ def build_um(build_dir):
var[1] = Z_1.T


def db_path(build_dir):
return os.path.join(build_dir, DB_FILE)

def build_database(build_dir):
db_path = os.path.join(build_dir, DB_FILE)
um_path = os.path.join(build_dir, UM_FILE)
if not os.path.exists(um_path):
build_um(build_dir)
print("building: {}".format(db_path))
database = forest.db.database.Database.connect(db_path)
print("building: {}".format(db_path(build_dir)))
database = forest.db.database.Database.connect(db_path(build_dir))
database.insert_netcdf(um_path)
database.close()

Expand Down
5 changes: 4 additions & 1 deletion test/test_drivers_unified_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def test_navigator_use_database(tmpdir):
}
dataset = forest.drivers.get_dataset("unified_model", settings)
navigator = dataset.navigator()
assert isinstance(navigator, forest.db.database.Database)
assert hasattr(navigator, "variables")
assert hasattr(navigator, "initial_times")
assert hasattr(navigator, "valid_times")
assert hasattr(navigator, "pressures")


def test_loader_use_database(tmpdir):
Expand Down
2 changes: 1 addition & 1 deletion test/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_build_all_builds_um_config_file(build_dir):
"pattern": "*" + forest.tutorial.core.UM_FILE,
"directory": build_dir,
"locator": "database",
"database_path": "database.db",
"database_path": f"{build_dir}/database.db",
},
},
}
Expand Down

0 comments on commit 8acd8b4

Please sign in to comment.