(365)
(8)
(130)
(155)
(50)
(22)
分类: python/ruby
2021-06-29 17:21:09
from pandas import excelwriter
import numpy as np
import os
import pandas as pd
import xml.etree.elementtree as et
# ucd_trn xml file path
xml_path = r"..\gcam-v5.2\input\extra" # xml file folder
dir_extra = os.listdir(xml_path) # ssp3 uses trn_ucd_ssp3
trn_xml_ls = [file for file in dir_extra if "transportation_ucd_ssp" in file]
trn_xml_path = dict(
zip(
["ssp" str(i) for i in [1, 2, 4, 5]],
[os.path.join(xml_path, file) for file in trn_xml_ls],
)
)
# car type
transubsector_name_ls = [
"compact car",
"large car and suv",
"mini car",
"multipurpose vehicle",
"subcompact car",
]
def get_ene_intensity_xml(path,
transubsector_name,
supplysector_name="trn_pass_road_ldv_4w"):
xtree = et.parse(path)
xroot = xtree.getroot()
rows = []
for child in xroot:
for region in child:
if region.attrib.get("name") == "china":
for supplysector in region:
if supplysector.attrib.get("name") == supplysector_name:
for transubsector in supplysector:
if transubsector.attrib.get("name") == transubsector_name:
for stubtechnology in transubsector:
if stubtechnology.attrib.get("name") == "fcev":
for period in stubtechnology:
for node in period:
if node.tag == "minicam-energy-input":
for sub_node in node:
if (
sub_node.tag
== "coefficient"
):
s_region = (
region.attrib.get(
"name"
)
)
s_supplysector = (
supplysector.attrib.get(
"name"
)
)
s_transubsector = transubsector.attrib.get(
"name"
)
s_stubtechnology = stubtechnology.attrib.get(
"name"
)
s_period = (
period.attrib.get(
"year"
)
)
s_tag = sub_node.tag
s_coefficient = (
float(sub_node.text)
/ 1055 # btu/vkm to j/vkm from
)
rows.append(
dict(
region=s_region,
supplysector=s_supplysector,
transubsector=s_transubsector,
stubtechnology=s_stubtechnology,
period=s_period,
tag=s_tag,
value=s_coefficient,
)
)
df = pd.dataframe(rows)
return df
def ene_intensity(scenario):
ene_intensity_ls = []
for car_type in transubsector_name_ls:
ene_intensity = np.array(
get_ene_intensity_xml(scenario, car_type).loc[:, "value"]
)[1:]
ene_intensity_ls.append(ene_intensity)
return ene_intensity_ls
columns = range(2000, 2101, 5) #读取2000到2100年能源强度,步长为五年
df = (
pd.dataframe(
ene_intensity(trn_xml_path[key]),
columns=columns,
index=transubsector_name_ls,
)
)
主要是搞清楚xml文档的数据结构,gcam交通部门中的结构为