能源系统建模:python读取gcam的xml数据文件-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3600104
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

(365)

  • (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) #读取20002100年能源强度,步长为五年

df = (

        pd.dataframe(

            ene_intensity(trn_xml_path[key]),

            columns=columns,

            index=transubsector_name_ls,

        )

    )

主要是搞清楚xml文档的数据结构,gcam交通部门中的结构为

# 能源强度

阅读(8574) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图