Converting a case file¶
The Dispatch API accepts historical NEMDE case files in their original XML format, so no conversion is required to solve an unmodified case file. However, converting a case file to a Python dictionary makes it easy to inspect and modify its parameters, and the dictionary can then be submitted to the API as JSON (see the previous tutorials).
Historical NEMDE case files are easily converted into this format by using the following code snippet.
import xmltodict
def convert_casefile(path_to_file):
"""Load a historical NEMDE case file and convert it to a dict"""
# Read case file contents
with open(path_to_file, 'r') as f:
casefile = f.read()
# Force these nodes to always return lists
force_list = ('Trade', 'TradeTypePriceStructure',)
return xmltodict.parse(casefile, force_list=force_list)
# Example NEMDE case file
path_to_file = '../../data/NEMSPDOutputs_2021040100100.loaded'
# Case file loaded as a Python dictionary
converted_casefile = convert_casefile(path_to_file=path_to_file)
converted_casefile.get('NEMSPDCaseFile').keys()
dict_keys(['NemSpdInputs', 'NemSpdOutputs', 'SolutionAnalysis'])
Note that historical case files have a .loaded suffix, with data within these files organised in XML format.
The force_list argument ensures Trade and TradeTypePriceStructure nodes are always parsed as lists, even when a case file contains only one such element. The Dispatch API uses the same setting when parsing XML case files, and expects case files submitted as JSON to follow this structure.