forked from NOAA-EMC/global-workflow
-
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.
Initial blocks in place for forecast refactor work (NOAA-EMC#1466)
This PR is a first in a series of PR for transforming the forecast job. This PR does not affect current function of the forecast job. This PR: - adds initial blocks to separate task specific and model configuration for the task blocks
- Loading branch information
Showing
8 changed files
with
166 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
|
||
from pygw.logger import Logger, logit | ||
from pygw.yaml_file import save_as_yaml | ||
from pygw.configuration import cast_strdict_as_dtypedict | ||
from pygfs.task.gfs_forecast import GFSForecast | ||
|
||
# initialize root logger | ||
logger = Logger(level=os.environ.get("LOGGING_LEVEL"), colored_log=True) | ||
|
||
|
||
@logit(logger) | ||
def main(): | ||
|
||
# instantiate the forecast | ||
config = cast_strdict_as_dtypedict(os.environ) | ||
save_as_yaml(config, f'{config.EXPDIR}/fcst.yaml') # Temporarily save the input to the Forecast | ||
|
||
fcst = GFSForecast(config) | ||
fcst.initialize() | ||
fcst.configure() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import logging | ||
from typing import Dict, Any | ||
|
||
from pygw.logger import logit | ||
from pygw.task import Task | ||
from pygfs.ufswm.gfs import GFS | ||
|
||
logger = logging.getLogger(__name__.split('.')[-1]) | ||
|
||
|
||
class GFSForecast(Task): | ||
""" | ||
UFS-weather-model forecast task for the GFS | ||
""" | ||
|
||
@logit(logger, name="GFSForecast") | ||
def __init__(self, config: Dict[str, Any], *args, **kwargs): | ||
""" | ||
Parameters | ||
---------- | ||
config : Dict | ||
dictionary object containing configuration from environment | ||
*args : tuple | ||
Additional arguments to `Task` | ||
**kwargs : dict, optional | ||
Extra keyword arguments to `Task` | ||
""" | ||
|
||
super().__init__(config, *args, **kwargs) | ||
|
||
# Create and initialize the GFS variant of the UFS | ||
self.gfs = GFS(config) |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import copy | ||
import logging | ||
|
||
from pygw.logger import logit | ||
from pygfs.ufswm.ufs import UFS | ||
|
||
logger = logging.getLogger(__name__.split('.')[-1]) | ||
|
||
|
||
class GFS(UFS): | ||
|
||
@logit(logger, name="GFS") | ||
def __init__(self, config): | ||
|
||
super().__init__("GFS", config) | ||
|
||
# Start putting fixed properties of the GFS | ||
self.ntiles = 6 | ||
|
||
# Determine coupled/uncoupled from config and define as appropriate |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import re | ||
import copy | ||
import logging | ||
from typing import Dict, Any | ||
|
||
from pygw.template import Template, TemplateConstants | ||
from pygw.logger import logit | ||
|
||
logger = logging.getLogger(__name__.split('.')[-1]) | ||
|
||
UFS_VARIANTS = ['GFS'] | ||
|
||
|
||
class UFS: | ||
|
||
@logit(logger, name="UFS") | ||
def __init__(self, model_name: str, config: Dict[str, Any]): | ||
"""Initialize the UFS-weather-model generic class and check if the model_name is a valid variant | ||
Parameters | ||
---------- | ||
model_name: str | ||
UFS variant | ||
config : Dict | ||
Incoming configuration dictionary | ||
""" | ||
|
||
# First check if this is a valid variant | ||
if model_name not in UFS_VARIANTS: | ||
logger.warn(f"{model_name} is not a valid UFS variant") | ||
raise NotImplementedError(f"{model_name} is not yet implemented") | ||
|
||
# Make a deep copy of incoming config for caching purposes. _config should not be updated | ||
self._config = copy.deepcopy(config) | ||
|
||
@logit(logger) | ||
def parse_ufs_templates(input_template, output_file, ctx: Dict) -> None: | ||
""" | ||
This method parses UFS-weather-model templates of the pattern @[VARIABLE] | ||
drawing the value from ctx['VARIABLE'] | ||
""" | ||
|
||
with open(input_template, 'r') as fhi: | ||
file_in = fhi.read() | ||
file_out = Template.substitute_structure( | ||
file_in, TemplateConstants.AT_SQUARE_BRACES, ctx.get) | ||
|
||
# If there are unrendered bits, find out what they are | ||
pattern = r"@\[.*?\]+" | ||
matches = re.findall(pattern, file_out) | ||
if matches: | ||
logger.warn(f"{input_template} was rendered incompletely") | ||
logger.warn(f"The following variables were not substituted") | ||
print(matches) # TODO: improve the formatting of this message | ||
# TODO: Should we abort here? or continue to write output_file? | ||
|
||
with open(output_file, 'w') as fho: | ||
fho.write(file_out) |
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