Software source reference
"""
Contains helper functions to fetch the absolute path of the parameters, and to read and write JSON files.
Authors:
Min Tai Yap
"""
from importlib import resources
import json
import os
[docs]
def get_path():
"""
Fetches the absolute path of the working directory, as well as the path to the parameter and results directory
:returns: Absolute path to the current working directory, the parameter directory, and the results directory
"""
curr_dir = os.getcwd()
param_path = os.path.join(curr_dir, 'parameter')
result_path = os.path.join(curr_dir, 'results')
return curr_dir, param_path, result_path
[docs]
def read_dict(filepath):
"""
Reads the JSON file located at the given path
:param filepath: Path to the JSON file
:returns: Dictionary of the contents of the JSON file
"""
with open(filepath) as f:
data = f.read()
dictionary = json.loads(data)
return dictionary
[docs]
def read_parameter_dict(json_filename):
"""
Reads JSON parameter files from the parameter folder when the software is installed as
a library
:param json_filename: file name of the JSON file in parameter
:returns: Dictionary that contains contents of the JSON file
"""
with resources.open_text("wind_chaser_software.parameter", json_filename) as f:
json_dict = json.load(f)
return json_dict
[docs]
def write_dict(filepath, data):
"""
Writes data to a JSON file in write mode
:param filepath: Path of the JSON file to create / overwrite
:param data: Data to be written to the JSON file
"""
with open(filepath, 'w') as convert_file:
convert_file.write(json.dumps(data))
