Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNew-NOAA committed Jul 24, 2024
1 parent 5b85c73 commit 754dd8c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
4 changes: 2 additions & 2 deletions scripts/exglobal_atmens_analysis_fv3_increment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os

from wxflow import Logger, cast_strdict_as_dtypedict
from pygfs.task.atmens_analysis_fv3inc import AtmEnsAnalysis
from pygfs.task.atmens_analysis_fv3inc import AtmEnsAnalysisFV3Inc

# Initialize root logger
logger = Logger(level='DEBUG', colored_log=True)
Expand All @@ -18,7 +18,7 @@
config = cast_strdict_as_dtypedict(os.environ)

# Instantiate the atmens analysis task
AtmEnsAnlFV3Inc = AtmEnsAnalysis(config)
AtmEnsAnlFV3Inc = AtmEnsAnalysisFV3Inc(config)
AtmEnsAnlFV3Inc.initialize()
AtmEnsAnlFV3Inc.execute(config.APRUN_ATMENSANLFV3INC)
AtmEnsAnlFV3Inc.finalize()
2 changes: 1 addition & 1 deletion ush/python/pygfs/task/atm_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from wxflow import (AttrDict,
FileHandler,
add_to_datetime, to_timedelta, to_YMDH,
add_to_datetime, to_fv3time, to_timedelta, to_YMDH,
parse_j2yaml,
logit)
from pygfs.task.jedi import JEDI
Expand Down
1 change: 1 addition & 0 deletions ush/python/pygfs/task/atm_analysis_fv3inc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AtmAnalysisFV3Inc(AtmAnalysis):
"""
@logit(logger, name="AtmAnalysisFV3Inc")
def __init__(self, config):
# Inherit task_config from AtmAnalysis class
super().__init__(config)

@logit(logger)
Expand Down
10 changes: 6 additions & 4 deletions ush/python/pygfs/task/atmens_analysis_fv3inc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import os
from logging import getLogger
from wxflow import FileHandler, to_fv3time, Template, TemplateConstants
from wxflow import logit

from pygfs.task.jedi import JEDI
from pygfs.task.atmens_analysis import AtmEnsAnalysis

logger = getLogger(__name__.split('.')[-1])
Expand All @@ -18,6 +19,7 @@ class AtmEnsAnalysisFV3Inc(AtmEnsAnalysis):
"""
@logit(logger, name="AtmEnsAnalysisFV3Inc")
def __init__(self, config):
# Inherit task_config from AtmEnsAnalysis class
super().__init__(config)

@logit(logger)
Expand All @@ -26,11 +28,11 @@ def initialize(self) -> None:

@logit(logger)
def execute(self, aprun_cmd: str) -> None:
JEDI.execute(aprun_cmd)
JEDI.execute(self, aprun_cmd)

@logit(logger)
def finalize(self) -> None:
JEDI.finalize()
JEDI.finalize(self)

def clean(self):
JEDI.clean()
JEDI.clean(self)
124 changes: 62 additions & 62 deletions ush/python/pygfs/task/jedi.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,72 +227,72 @@ def get_bias_dict(self) -> Dict[str, Any]:
}
return bias_dict

@logit(logger)
def find_value_in_nested_dict(nested_dict: Dict, target_key: str) -> Any:
"""
Recursively search through a nested dictionary and return the value for the target key.
This returns the first target key it finds. So if a key exists in a subsequent
nested dictionary, it will not be found.
Parameters
----------
nested_dict : Dict
Dictionary to search
target_key : str
Key to search for
Returns
-------
Any
Value of the target key
Raises
------
KeyError
If key is not found in dictionary
TODO: if this gives issues due to landing on an incorrect key in the nested
dictionary, we will have to implement a more concrete method to search for a key
given a more complete address. See resolved conversations in PR 2387
# Example usage:
nested_dict = {
'a': {
'b': {
'c': 1,
'd': {
'e': 2,
'f': 3
}
},
'g': 4
},
'h': {
'i': 5
@logit(logger)
def find_value_in_nested_dict(nested_dict: Dict, target_key: str) -> Any:
"""
Recursively search through a nested dictionary and return the value for the target key.
This returns the first target key it finds. So if a key exists in a subsequent
nested dictionary, it will not be found.
Parameters
----------
nested_dict : Dict
Dictionary to search
target_key : str
Key to search for
Returns
-------
Any
Value of the target key
Raises
------
KeyError
If key is not found in dictionary
TODO: if this gives issues due to landing on an incorrect key in the nested
dictionary, we will have to implement a more concrete method to search for a key
given a more complete address. See resolved conversations in PR 2387
# Example usage:
nested_dict = {
'a': {
'b': {
'c': 1,
'd': {
'e': 2,
'f': 3
}
},
'j': {
'k': 6
}
'g': 4
},
'h': {
'i': 5
},
'j': {
'k': 6
}
}
user_key = input("Enter the key to search for: ")
result = find_value_in_nested_dict(nested_dict, user_key)
"""
user_key = input("Enter the key to search for: ")
result = find_value_in_nested_dict(nested_dict, user_key)
"""

if not isinstance(nested_dict, dict):
raise TypeError(f"Input is not of type(dict)")
if not isinstance(nested_dict, dict):
raise TypeError(f"Input is not of type(dict)")

result = nested_dict.get(target_key)
if result is not None:
return result
result = nested_dict.get(target_key)
if result is not None:
return result

for value in nested_dict.values():
if isinstance(value, dict):
try:
result = find_value_in_nested_dict(value, target_key)
if result is not None:
return result
except KeyError:
pass
for value in nested_dict.values():
if isinstance(value, dict):
try:
result = find_value_in_nested_dict(value, target_key)
if result is not None:
return result
except KeyError:
pass

raise KeyError(f"Key '{target_key}' not found in the nested dictionary")
raise KeyError(f"Key '{target_key}' not found in the nested dictionary")

0 comments on commit 754dd8c

Please sign in to comment.