-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
50 lines (34 loc) · 1.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import datetime
import logging
import os
import tempfile
from octue.resources import Datafile, Dataset
from octue.utils.threads import run_logged_subprocess
logger = logging.getLogger(__name__)
OUTPUT_EXTENSION = ".bts"
OUTPUT_FILENAME = "TurbSim" + OUTPUT_EXTENSION
def run(analysis):
"""Run a turbsim analysis on the input file specified in the input manifest, writing the output file to the cloud.
:param octue.resources.Analysis analysis:
:return None:
"""
start_datetime = datetime.datetime.now()
with tempfile.TemporaryDirectory() as temporary_directory:
input_dataset = analysis.input_manifest.datasets["turbsim"]
input_dataset.download(temporary_directory)
input_file = input_dataset.files.one()
logger.info("Starting turbsim analysis.")
run_logged_subprocess(command=["turbsim", input_file.local_path], logger=logger)
old_output_filename = os.path.splitext(input_file.local_path)[0] + OUTPUT_EXTENSION
with tempfile.TemporaryDirectory() as new_temporary_directory:
new_output_filename = os.path.join(new_temporary_directory, OUTPUT_FILENAME)
os.rename(old_output_filename, new_output_filename)
# Instantiate a datafile for the output file.
with Datafile(path=new_output_filename, mode="a") as (datafile, f):
datafile.timestamp = start_datetime
datafile.labels = ["turbsim", "output"]
analysis.output_manifest.datasets["turbsim"] = Dataset(name="turbsim", path=new_temporary_directory)
# Explicitly call `finalise` here instead of relying on implicit finalisation so the temporary directory
# still exists when it's called.
analysis.finalise()
logger.info("Finished turbsim analysis.")