-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,69 @@ | ||
import nwp_consumer.cmd.main as consumer | ||
import contextlib | ||
import os | ||
from dagster import Config, OpExecutionContext, op | ||
from dagster_docker import execute_docker_container | ||
|
||
@contextlib.contextmanager | ||
def modify_env(vars: dict[str, str]): | ||
"""Temporarily modify the environment.""" | ||
for var in vars: | ||
oldval = os.environ.get(var) | ||
newval = vars[var] | ||
os.environ[var] = newval | ||
vars[var] = oldval | ||
try: | ||
yield | ||
finally: | ||
for var in vars: | ||
os.environ[var] = oldval | ||
|
||
class NWPConsumerConfig(Config): | ||
"""Configuration for the NWP consumer.""" | ||
|
||
date_from: str | ||
date_to: str | ||
source: str | ||
docker_volumes: list[str] | ||
raw_dir: str | ||
zarr_dir: str | ||
env_vars: list[str] | ||
|
||
@op | ||
def nwp_consumer_docker_op(context: OpExecutionContext, config: NWPConsumerConfig): | ||
execute_docker_container( | ||
context=context, | ||
image="ghcr.io/openclimatefix/nwp-consumer:latest", | ||
command=[ | ||
"consume", f'--source={config.source}', | ||
f'--from={config.date_from}', | ||
f'--to={config.date_to}' | ||
], | ||
env_vars=config.env_vars, | ||
container_kwargs={ | ||
"volumes": config.docker_volumes | ||
} | ||
) | ||
env_overrides: dict[str, str] | ||
|
||
pass | ||
|
||
@op | ||
def nwp_consumer_download_op(context: OpExecutionContext, config: NWPConsumerConfig): | ||
def nwp_consumer_download_op(context: OpExecutionContext, config: NWPConsumerConfig) \ | ||
-> NWPConsumerConfig: | ||
"""Download the data from the source.""" | ||
consumer.run({ | ||
"download": True, | ||
"convert": False, | ||
"consume": False, | ||
"check": False, | ||
"--source": config.source, | ||
"--sink": "local", | ||
"--from": config.date_from, | ||
"--to": config.date_to, | ||
"--rdir": config.raw_dir, | ||
"--zdir": config.zarr_dir, | ||
"--create-latest": False, | ||
}) | ||
with modify_env(config.env_overrides): | ||
consumer.run({ | ||
"download": True, | ||
"convert": False, | ||
"consume": False, | ||
"check": False, | ||
"--source": config.source, | ||
"--sink": "local", | ||
"--from": config.date_from, | ||
"--to": config.date_to, | ||
"--rdir": config.raw_dir, | ||
"--zdir": config.zarr_dir, | ||
"--create-latest": False, | ||
}) | ||
|
||
return config | ||
|
||
@op | ||
def nwp_consumer_convert_op(context: OpExecutionContext, downloadedConfig: NWPConsumerConfig): | ||
"""Convert the downloaded data to zarr format.""" | ||
consumer.run({ | ||
"download": False, | ||
"convert": True, | ||
"consume": False, | ||
"check": False, | ||
"--source": downloadedConfig.source, | ||
"--sink": "local", | ||
"--from": downloadedConfig.date_from, | ||
"--to": downloadedConfig.date_to, | ||
"--rdir": downloadedConfig.raw_dir, | ||
"--zdir": downloadedConfig.zarr_dir, | ||
"--create-latest": False, | ||
}) | ||
with modify_env(downloadedConfig.env_overrides): | ||
consumer.run({ | ||
"download": False, | ||
"convert": True, | ||
"consume": False, | ||
"check": False, | ||
"--source": downloadedConfig.source, | ||
"--sink": "local", | ||
"--from": downloadedConfig.date_from, | ||
"--to": downloadedConfig.date_to, | ||
"--rdir": downloadedConfig.raw_dir, | ||
"--zdir": downloadedConfig.zarr_dir, | ||
"--create-latest": False, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters