def validate_inputs(input_dict: dict) -> None: """ Validates the input objects against the schema. :param input_dict: input object :raises ValidationError if the input object is not correct :raises SchemaError is the schema is not correct """ # shortcut fpath = os.path.join for key, value in input_dict.items(): # load proper the schema schema_path = fpath(os.path.dirname(os.path.abspath(__file__)), 'schema') schema = load_json(fpath(schema_path, '{}.jsonschema'.format(key))) # validate try: validate(lower_obj(value), lower_obj(schema)) except ValidationError: raise ValidationError( "Input object '{}' is not valid.".format(key)) except SchemaError: # pragma: no cover raise SchemaError( "Schema for object '{}' is not valid.".format( key)) # pragma: no cover
def write_no_agg_json_input(run_dir, time, path_to_load, path_to_g, path_to_output): f = load_json(norm(join(cwd, '../base', 'base_case.json'))) f['flow-profile']['external']['path'] = path_to_load f['load-profile']['external']['path'] = path_to_load f['g-functions']['file'] = path_to_g f['simulation']['runtime'] = time f['simulation']['output-path'] = path_to_output write_json(join(run_dir, 'in.json'), f)
def test_write_json(self): temp_directory = tempfile.mkdtemp() temp_file = os.path.join(temp_directory, 'temp.json') d = {"key": "value", "key 2": 2} write_json(temp_file, d) loaded = load_json(temp_file) self.assertEqual(d, loaded)
def test_load_json(self): temp_directory = tempfile.mkdtemp() temp_json_file = os.path.join(temp_directory, 'temp.json') with open(temp_json_file, 'w') as f: f.write('{"key": "value", "key 2": 1}') d = load_json(temp_json_file) self.assertEqual(d["key"], "value") self.assertEqual(d["key 2"], 1)
def test_simulate(self): temp_dir = tempfile.mkdtemp() temp_file = join(temp_dir, 'in.json') input_path = norm( join(self.this_file_directory, '..', '..', 'test_files', 'single.json')) d = load_json(input_path) g_path = norm( join(self.this_file_directory, '..', '..', 'test_files', 'single_g_functions.csv')) d['ground-heat-exchanger'][0]['g-function-path'] = g_path d['simulation']['output-path'] = temp_dir write_json(temp_file, d) p = PlantLoop(temp_file) self.assertTrue(p.simulate())
def get_base_run_file_path(path): d = load_json(path) load_path_str = d['flow-profile']['external']['path'] load = os_path_split_asunder(load_path_str)[-1].split(".")[0] time = int(d['simulation']['runtime'] / SEC_IN_YEAR) this_file_path = os.path.dirname(__file__) path_to_base_run_file = norm( join(this_file_path, '..', 'none', 'runs', '{}_{}'.format(load, time))) if not os.path.exists(path_to_base_run_file): raise FileNotFoundError return path_to_base_run_file, load, time
def write_dynamic_json_input(run_dir, time, path_to_load, path_to_g, path_to_output, other): f = load_json(norm(join(cwd, '../base', 'base_case.json'))) f['flow-profile']['external']['path'] = path_to_load f['load-profile']['external']['path'] = path_to_load f['g-functions']['file'] = path_to_g f['simulation']['runtime'] = time f['simulation']['output-path'] = path_to_output f['load-aggregation']['type'] = 'dynamic' f['load-aggregation']['dynamic']['depth'] = other['depth'] f['load-aggregation']['dynamic']['expansion rate'] = other['expansion rate'] f['load-aggregation']['dynamic']['start width'] = other['start width'] f['load-aggregation']['dynamic']['end width'] = other['end width'] write_json(join(run_dir, 'in.json'), f)
def write_static_json_input(run_dir, time, path_to_load, path_to_g, path_to_output, other): f = load_json(norm(join(cwd, '../base', 'base_case.json'))) f['flow-profile']['external']['path'] = path_to_load f['load-profile']['external']['path'] = path_to_load f['g-functions']['file'] = path_to_g f['simulation']['runtime'] = time f['simulation']['output-path'] = path_to_output f['load-aggregation']['type'] = 'static' f['load-aggregation']['static']['min number bins'] = other[ 'min number bins'] f['load-aggregation']['static']['bin widths in hours'] = other[ 'bin widths in hours'] write_json(join(run_dir, 'in.json'), f)
def write_annual_json_input(run_dir, time, path_to_load, path_to_g, path_to_output, other): f = load_json(norm(join(cwd, '../base', 'base_case.json'))) f['flow-profile']['external']['path'] = path_to_load f['load-profile']['external']['path'] = path_to_load f['g-functions']['file'] = path_to_g f['simulation']['runtime'] = time f['simulation']['output-path'] = path_to_output f['load-aggregation']['type'] = 'none' f['soil']['conductivity'] = other['k'] f['soil']['density'] = other['rho'] f['soil']['specific heat'] = other['cp'] write_json(join(run_dir, 'in.json'), f)
def __init__(self, json_input_path: str) -> None: """ Initialize the input processor, process input file, and store the input information. :raises FileNotFoundError when input file not found. :param json_input_path: input file path """ # check if file exists if not os.path.exists(json_input_path): raise FileNotFoundError( "Input file: '{}' does not exist.".format(json_input_path)) # load the input file self.input_dict = lower_obj(load_json(json_input_path)) # validate the inputs self.validate_inputs(self.input_dict) # load properties for later use self.props_mgr = PropsMGR() self.props_mgr.load_properties(self.input_dict)