Skip to content

Commit

Permalink
Merge pull request #84 from UW-SASWE/sm-main
Browse files Browse the repository at this point in the history
Added RAT Toolbox Module for providing user helpful functions
  • Loading branch information
SanchitMinocha authored Nov 21, 2023
2 parents 3c405de + aea1ff1 commit 04185a8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/QuickStart/GettingReady.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ Click on ‘REGISTER SERVICE ACCOUNT’. <br>

### RAT Credentials

RAT {{rat_version.major}}.{{rat_version.minor}} requires all the credentials created in the above sections in a single secret file. So, put all the credentials into a file named 'secrets.ini' by following the commands as mentioned [here in secrets section](../Configuration/secrets.md).
RAT {{rat_version.major}}.{{rat_version.minor}} requires all the credentials created in the above sections in a single secret file. So, put all the credentials into a file named 'secrets.ini' by following the commands as mentioned [here in secrets file section](../Configuration/secrets.md).
3 changes: 2 additions & 1 deletion src/rat/cli/rat_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def init_func(args):
global_data_dir = global_data_parent_dir.joinpath('global_data')
# if not downloading global data
else:
global_data = 'N'
# and global_data directory is specified
if args.global_data_dir is not None:
global_data_dir = Path(args.global_data_dir).resolve()
Expand Down Expand Up @@ -160,9 +161,9 @@ def init_func(args):
print("Failed to download parameter files for RAT.")

#### download global data
print("Downloading global database for RAT...")
try:
if global_data == 'Y':
print("Downloading global database for RAT...")
if drive=='dropbox':
global_data_dl_url = DOWNLOAD_LINKS_DROPBOX["global_data"]
global_vic_params_dl_url = DOWNLOAD_LINKS_DROPBOX["global_vic_params"]
Expand Down
Empty file added src/rat/toolbox/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions src/rat/toolbox/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ruamel_yaml as ryaml
from pathlib import Path

#Function to update configuration file given a parameters dictionary
def update_config(file_path, update_params):
"""
Update RAT's existing configuration file in YAML format .
Parameters:
- file_path (str): The path to the configuration file.
- update_params (dict): A nested dictionary containing the parameters to update.
Example:
update_params = {'GLOBAL': {'steps': [1,2], 'multiprocessing': 4},
'BASIN': {'basin_name': 'ganga'}}
update_config('path/to/config.yml', update_params)
"""
try:
# Reading config with comments
config_file_path = Path(file_path).resolve()
ryaml_client = ryaml.YAML()
config = ryaml_client.load(config_file_path.read_text())
except FileNotFoundError:
raise FileNotFoundError(f"The file '{file_path}' does not exist.")

# Update the configuration with the new parameters
for section, params in update_params.items():
# If a section is not already present, add section with one line space at the end
if section not in config:
# Add a CommentedMap with one line space from the previous section
prev_section = list(config.keys())[-1] if config else None
config[section] = ryaml.comments.CommentedMap()
if prev_section:
config.yaml_set_comment_before_after_key(
section, before='\n', after=''
)
# Updating keys in each section
for key, value in params.items():
config[section][key] = value

# Write the updated configuration back to the file
ryaml_client.dump(config, config_file_path.open('w'))

0 comments on commit 04185a8

Please sign in to comment.