General utilities
read_config
read_config(config_file: Union[str, Path]) -> ConfigParser
Read a .ini config file and return a ConfigParser object.
Parameters
config_file : str, Path Absolute path to the config file to be read. The config file should be in the .ini format [1].
Returns
configparser.ConfigParser
A ConfigParser object containing the configuration parameters.
To access the parameters, use the config.get(section, option) method,
where section is the name of the section in the config file and option
is the name of the option within that section.
Source code in tinkertool/utils/read_files.py
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 | |
safe_get_param_value
safe_get_param_value(
config_section, option: str, fallback=None
) -> Any
Get a parameter value from config, converting 'nan', 'none', 'null', '' strings to None or fallback.
Parameters
config_section : configparser.SectionProxy The config section to read from option : str The option name to get fallback : any, optional Value to return if option doesn't exist or is nan/none/null/empty string, by default None
Returns
Any The parameter value, or None if it was a nan/none/null/empty string
Source code in tinkertool/utils/read_files.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
check_if_ctsm_param_is_perturbed
check_if_ctsm_param_is_perturbed(
param_ranges_inpath: str | Path,
) -> bool
Check if any CTSM parameters are perturbed in the parameter ranges file.
Parameters
param_ranges_inpath : str | Path The path to the parameter ranges file.
Returns
bool True if any CTSM parameters are perturbed, False otherwise.
Source code in tinkertool/utils/make_land_parameterfiles.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
check_if_fates_param_is_perturbed
check_if_fates_param_is_perturbed(
param_ranges_inpath: str | Path,
) -> bool
Check if any FATES parameters are perturbed in the parameter ranges file.
Parameters
param_ranges_inpath : str | Path The path to the parameter ranges file.
Returns
bool True if any FATES parameters are perturbed, False otherwise.
Source code in tinkertool/utils/make_land_parameterfiles.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
make_new_ctsm_pamfile
make_new_ctsm_pamfile(
pam_change_dict: dict,
orig_pamfile: str | Path,
file_dump: str | Path = "ctsm_pamfile.nc",
) -> Path
Make a new ctsm pamfile for the PPE, by changing parameters as specified in pam_change_dict.
Parameters
pam_change_dict : dict Dictionary with parameter names as keys and new values or scaling factors as values. The pam_change_dict has parameter should have name of parameter to change as key, and change value as value. For scalar parameters this is assumed to be the new value of the parameter. For vector parameters the value is assumed to be a scaling value to scale the value in the original file with orig_pamfile : str or Path Path to the original CTSM parameter file to be modified. file_dump : str or Path, optional Path to save the new CTSM parameter file, by default "ctsm_pamfile.nc".
Source code in tinkertool/utils/make_land_parameterfiles.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
make_new_fates_pamfile
make_new_fates_pamfile(
pam_change_dict: dict,
orig_pamfile: str | Path,
file_dump: str | Path = "fates_pamfile.nc",
) -> Path
Make a new fates pamfile for the PPE, by changing parameters as specified in pam_change_dict.
Parameters
pam_change_dict : dict Dictionary with parameter names as keys and new values or scaling factors as values. The pam_change_dict has parameter should have name of parameter to change as key, and change value as value. For scalar parameters this is assumed to be the new value of the parameter. For vector parameters the value is assumed to be a scaling value to scale the value in the original file with. orig_pamfile : str | Path Path to the original FATES parameter file to be modified. file_dump : str | Path, optional Path to save the new FATES parameter file, by default "fates_pamfile.nc".
Returns
Path Path to the new FATES parameter file.
Raises
FileNotFoundError If the original FATES parameter file is not found.
Source code in tinkertool/utils/make_land_parameterfiles.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
generate_chem_in_ppe
generate_chem_in_ppe(
scale_factor: float,
input_file: str | Path,
outfolder_base: str | Path,
outfolder_name: str,
verbose: bool = False,
) -> str
Generate a chemistry namelist file for a given scale factor and input file.
Parameters:
scale_factor : float The scale factor to use for the chemistry file. input_file : str | Path The input file to use for the chemistry file. outfolder_base : str | Path The base folder to use for the output files. outfolder_name : str The name of the folder to use for the output files. verbose : bool, optional If True, print verbose output. Default False.
Returns
str The path to the generated chemistry file.
Source code in tinkertool/utils/make_chem_in.py
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
check_if_chem_mech_is_perturbed
check_if_chem_mech_is_perturbed(param_ranges: dict) -> bool
Check if the chemistry mechanism is perturbed. The check is performed by looking for specific section headers defined in chem_mech_variable_flags in the parameter ranges file.
Parameters
param_ranges_inpath : str | Path The path to the parameter ranges file.
Returns
bool True if the chemistry mechanism is perturbed, False otherwise.
Source code in tinkertool/utils/make_chem_in.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
patch_info_detailed
patch_info_detailed()
Patch Logger class to add info_detailed method and custom level.
Source code in tinkertool/utils/custom_logging.py
10 11 12 13 14 15 16 17 18 | |
log_info_detailed
log_info_detailed(logger_name: str, message: str)
Helper function to log info_detailed messages with proper type handling.
Source code in tinkertool/utils/custom_logging.py
20 21 22 23 24 25 26 | |
setup_logging
setup_logging(
verbosity: int,
log_file: Optional[Union[str, Path]] = None,
log_mode: str = "w",
logger_name: str = "tinkertool_log",
)
Set up logging configuration. Both for the root logger and a custom logger.
Parameters
verbosity : int Verbosity level for logging. 0 for WARNING, 1 for INFO, 2 for INFO_DETAILED, 3 for DEBUG. log_file : str or Path, optional Path to the log file where logs will be written. If None, logs will not be saved to a file. Default is None. log_mode : str Mode for opening the log file. 'w' for write (overwrite), 'a' for append. Default is 'w'. logger_name : str Name of the logger. Default is 'tinkertool_log'.
Source code in tinkertool/utils/custom_logging.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
cime_logger
cime_logger(
verbosity: int,
log_file: Optional[Union[str, Path]] = None,
log_mode: str = "w",
)
Set up the CIME logger with a stream handler and an optional file handler.
If a log file is provided, the CIME logger writes to a file with the same name as log_file,
but with .CIME inserted before the suffix (e.g., build_ppe.log → build_ppe.CIME.log).
Parameters
verbosity : int
Verbosity level for logging. 0 for WARNING, 1 for INFO, 3 for DEBUG.
log_file : str or Path, optional
Path to the base log file. If provided, the CIME logger writes to a file with
.CIME added to the stem (e.g., mylog.log → mylog.CIME.log). If None, logs
are not written to a file.
log_mode : str, default 'w'
Mode to open the log file. 'w' for overwrite, 'a' for append.
Returns
None
Examples
cime_logger(1, Path("output.log"), "w")
Logs to both stdout and 'output.CIME.log' at INFO level.
cime_logger(0)
Logs only to stdout at WARNING level.
Source code in tinkertool/utils/custom_logging.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
custom_logging
custom_logging(
verbosity: int,
log_file: Optional[Union[str, Path]] = None,
log_mode: str = "w",
logger_name: str = "tinkertool_log",
)
Set up logging configuration. for a custom logger with a custom level.
Parameters
verbosity : int Verbosity level for logging. 0 for WARNING, 1 for INFO, 2 for INFO_DETAILED, 3 for DEBUG. log_file : str or Path, optional Path to the log file where logs will be written. If None, logs will not be saved to a file. Default is None. log_mode : str Mode for opening the log file. 'w' for write (overwrite), 'a' for append. Default is 'w'. logger_name : str Name of the logger. Default is 'tinkertool_log'.
Source code in tinkertool/utils/custom_logging.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
input_with_timer
input_with_timer(
prompt: str, timeout: int, default: Optional[str] = None
) -> Optional[str]
Get user input with a timeout.
Parameters
prompt : str The prompt message to display to the user. timeout : int The time in seconds to wait for user input before timing out. default : str, optional The default value to return if the timeout is reached. Default is None.
Returns
str or None The user input if provided within the timeout, otherwise the default value.
Source code in tinkertool/utils/custom_logging.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
type_check_decorator
type_check_decorator(func)
Decorator [1] for type checking of user input to functions at runtime. Known limitations: - Does not support parameterized typechecking as it is based on isinstance [3]. That is does not support hints like list[int] or numpy._typing._array_like._SupportsArray[numpy.dtype[typing.Any]], etc. Therefor aliases like numpy.typing.ArrayLike are not supported. The workaround is to be more specific with the type hints and use things like Union[int, list, numpy.ndarray].
Parameters
func : Callable A function with implemented type hints [2].
Source code in tinkertool/utils/type_check_decorator.py
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 | |