def test_write_parameter_skip_defaults(self): data = [ ["SIMPLICITY", "BIOMASS", 0.95969], ["SIMPLICITY", "ETH1", 4.69969], ["SIMPLICITY", "ETH2", -1], ["SIMPLICITY", "ETH3", -1], ] df = pd.DataFrame(data=data, columns=["REGION", "FUEL", "VALUE"]) stream = io.StringIO() convert = WriteDatafile() convert._write_parameter(df, "test_parameter", stream, -1) stream.seek(0) expected = [ "param default -1 : test_parameter :=\n", "SIMPLICITY BIOMASS 0.95969\n", "SIMPLICITY ETH1 4.69969\n", ";\n", ] actual = stream.readlines() for actual_line, expected_line in zip(actual, expected): assert actual_line == expected_line
def test_write_set(self): data = [["BIOMASS"], ["ETH1"]] df = pd.DataFrame(data=data, columns=["VALUE"]) stream = io.StringIO() convert = WriteDatafile() convert._write_set(df, "TECHNOLOGY", stream) stream.seek(0) expected = ["set TECHNOLOGY :=\n", "BIOMASS\n", "ETH1\n", ";\n"] actual = stream.readlines() for actual_line, expected_line in zip(actual, expected): assert actual_line == expected_line
def test_write_empty_parameter_with_defaults(self): convert = WriteDatafile() # typing: WriteDatafile data = [] df = pd.DataFrame(data=data, columns=["REGION", "FUEL", "VALUE"]) stream = io.StringIO() convert._write_parameter(df, "test_parameter", stream, 0) stream.seek(0) expected = ["param default 0 : test_parameter :=\n", ";\n"] actual = stream.readlines() for actual_line, expected_line in zip(actual, expected): assert actual_line == expected_line
def use_otoole(config_dict): """ Use otoole to create OSeMOSYS data package. """ subset_of_years = config_dict['years'] # prepare using otoole _path='./tmp/combined_data.xlsx' reader = ReadExcel() writer = WriteDatafile() data, default_values = reader.read(_path) # edit data (the dict of dataframes) with resources.open_text('aperc_osemosys','data_config.yml') as open_file: contents = yaml.load(open_file, Loader=yaml.FullLoader) filtered_data2 = {} for key,value in contents.items(): _df = data[key] if contents[key]['type'] == 'param': if ('YEAR' in contents[key]['indices']): #print('parameters with YEAR are.. {}'.format(key)) _df2 = _df.query('YEAR < @subset_of_years+1') filtered_data2[key] = _df2 else: #print('parameters without YEAR are.. {}'.format(key)) filtered_data2[key] = _df elif contents[key]['type'] == 'set': if key == 'YEAR': _df2 = _df.query('VALUE < @subset_of_years+1') filtered_data2[key] = _df2 else: #print('sets are.. {}'.format(key)) filtered_data2[key] = _df else: filtered_data2[key] = _df output_file = './tmp/datafile_from_python.txt' writer.write(filtered_data2, output_file, default_values) return