cuwalid.tools package
Submodules
cuwalid.tools.CUWALID_download_data module
cuwalid.tools.CUWALID_json_builder module
cuwalid.tools.CUWALID_make_dirs module
cuwalid.tools.CUWALID_mfile_tools module
cuwalid.tools.CUWALID_view_tool module
cuwalid.tools.DRYP_convert module
- cuwalid.tools.DRYP_convert.convert_text_to_json(input_file, output_file=None)[source]
Converts a text file to a JSON file based on its format.
This function reads an input text file, determines its format based on the first line, and parses the content into a predefined JSON structure. The converted JSON is saved to a specified output file. If the conversion is successful, the original input file is deleted.
- Parameters:
input_file (str) – Path to the input text file to be converted. The file should be in either DWAPM_SET or drylandmodel format.
output_file (str, optional) – Path to the output JSON file. If not provided, the output file is named based on the input file name with a ‘.json’ extension.
- Returns:
The function does not return any value. It saves the converted JSON to a file and may delete the input file if the conversion is successful.
- Return type:
None
- Raises:
Exception – If there is an error during file saving or deletion, an exception is printed.
Notes
The function supports two formats:
DWAPM_SET: The input file is expected to have settings in a specific format. The corresponding JSON structure and mappings for this format are predefined.
drylandmodel: The input file is expected to describe a dryland model. The function parses the file into a predefined JSON structure based on mappings for this format.
Example
convert_text_to_json(‘path/to/input_file.txt’, ‘path/to/output_file.json’)