tfse-etl-api-v0.2/RatingData/scripts/esg/environment.py

425 lines
16 KiB
Python

import operator
def environment_co2(param):
"""
二氧化碳
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
income_data = param['公司当年收入(万元)']
three_year_data = sorted(param['环境问卷']['近三年公司数据'], key=operator.itemgetter('年份'), reverse=False)
# Returns
result = dict()
def date_range():
"""
评价周期年度区间
Parameters:
param dict 环境问卷
Returns:
result str 区间数据
"""
year_begin = three_year_data[0]['年份']
year_end = three_year_data[2]['年份']
res = year_begin + '-' + year_end
return res
def co2_emissions(year_param, income):
"""
公司年度总二氧化碳排放
Parameters:
year_param dict 年度环境问卷数据
Returns:
result str 该年度二氧化碳排放总量
"""
total = list()
total.append(round(year_param['公司当年消耗的原煤(千克)'] * 1.9003, 2))
total.append(round(year_param['公司当年消耗的焦炭(千克)'] * 2.8604, 2))
total.append(round(year_param['公司当年消耗的原油(升)'] * 0.86 * 3.0202, 2))
total.append(round(year_param['公司当年消耗的燃料油(升)'] * 0.86 * 3.1705, 2))
total.append(round(year_param['公司当年消耗的汽油(升)'] * 0.73 * 2.9251, 2))
total.append(round(year_param['公司当年消耗的煤油(升)'] * 0.82 * 3.0179, 2))
total.append(round(year_param['公司当年消耗的柴油(升)'] * 0.85 * 3.0959, 2))
total.append(round(year_param['公司当年消耗的液化石油气(千克)'] * 3.1013, 2))
total.append(round(year_param['公司当年消耗的天然气(立方米)'] * 2.1622, 2))
total.append(round(year_param['公司当年消耗的煤气(立方米)'] * 0.7100, 2))
total.append(round(year_param['公司当年消耗的电(千瓦时)'] * 0.6101, 2))
# 每年排放总量
amount = sum(total)
res = dict()
res['amount'] = amount
# 每年每万元收入二氧化碳排放量
# res['money_amount'] = round(amount / income, 2)
num = 2
while round(amount / income, num) == 0.0:
num += 1
res['money_amount'] = round(amount / income, num+1)
return res
def calculate_three_year_data():
"""
计算三年数据并转为字符串
Parameters:
'-'
Returns:
result str 三年数据用顿号隔开
"""
res = dict()
year_co2 = list()
year_thousand_co2 = list()
for index in range(len(three_year_data)):
co2_res = co2_emissions(three_year_data[index], income_data[three_year_data[index]['年份']])
year_co2.append(co2_res['amount'])
year_thousand_co2.append(co2_res['money_amount'])
# 列表中元素类型float转str
year_co2_new = [str(x) for x in year_co2]
year_thousand_new = [str(x) for x in year_thousand_co2]
# Returns-part1
res['part_01'] = dict()
res['part_01']['date_range'] = date_range()
res['part_01']['co2_emissions'] = ''.join(year_co2_new)
# part2
res['part_02'] = dict()
res['part_02']['date_range'] = date_range()
res['part_02']['co2_emissions'] = ''.join(year_thousand_new)
res['part_02']['year_02'] = three_year_data[1]['年份']
res['part_02']['year_03'] = three_year_data[2]['年份']
res['part_02']['year01_on_year02'] = round((year_thousand_co2[1]/year_thousand_co2[0] - 1) * 100, 2)
res['part_02']['year02_on_year03'] = round((year_thousand_co2[2]/year_thousand_co2[1] - 1) * 100, 2)
res['part_02']['industry_indicators'] = None
res['part_02']['industry_indicators_average'] = None
res['part_02']['industry_indicators_median'] = None
res['part_02']['median_comparison'] = None
return res
result['part_01'] = calculate_three_year_data()['part_01']
result['part_02'] = calculate_three_year_data()['part_02']
return result
def environment_energy(param):
"""
能耗
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
income_data = param['公司当年收入(万元)']
three_year_data = sorted(param['环境问卷']['近三年公司数据'], key=operator.itemgetter('年份'), reverse=False)
# Returns
result = dict()
def date_range():
"""
评价周期年度区间
Parameters:
param dict 环境问卷
Returns:
result str 区间数据
"""
year_begin = three_year_data[0]['年份']
year_end = three_year_data[2]['年份']
res = year_begin + '-' + year_end
return res
def ten_thousand_yuan_energy(year_param, income):
"""
公司年度能耗
Parameters:
year_param dict 年度环境问卷数据
Returns:
result str 该年度二氧化碳排放总量
"""
total = list()
total.append(round(year_param['公司当年消耗的原煤(千克)'] * 20.9080, 2))
total.append(round(year_param['公司当年消耗的焦炭(千克)'] * 28.4350, 2))
total.append(round(year_param['公司当年消耗的原油(升)'] * 0.86 * 41.8160, 2))
total.append(round(year_param['公司当年消耗的燃料油(升)'] * 0.86 * 41.8160, 2))
total.append(round(year_param['公司当年消耗的汽油(升)'] * 0.73 * 43.0700, 2))
total.append(round(year_param['公司当年消耗的煤油(升)'] * 0.82 * 43.0700, 2))
total.append(round(year_param['公司当年消耗的柴油(升)'] * 0.85 * 42.6520, 2))
total.append(round(year_param['公司当年消耗的液化石油气(千克)'] * 50.1790, 2))
total.append(round(year_param['公司当年消耗的天然气(立方米)'] * 38.9310, 2))
total.append(round(year_param['公司当年消耗的煤气(立方米)'] * 17.0000, 2))
total.append(round(year_param['公司当年消耗的电(千瓦时)'] * 11.8400, 2))
amount = sum(total)
res = dict()
res['amount'] = amount
num = 2
while round(amount / income, num) == 0.0:
num += 1
res['money_amount'] = round(amount / income, num + 1)
return res
def calculate_three_year_data():
"""
计算三年数据并转为字符串
Parameters:
'-'
Returns:
result str 三年数据用顿号隔开
"""
res = dict()
year_energy = list()
year_thousand_energy = list()
for index in range(len(three_year_data)):
energy_res = ten_thousand_yuan_energy(three_year_data[index], income_data[three_year_data[index]['年份']])
year_energy.append(energy_res['amount'])
year_thousand_energy.append(energy_res['money_amount'])
# 列表中元素类型float转str
year_energy_new = [str(x) for x in year_energy]
year_thousand_energy_new = [str(x) for x in year_thousand_energy]
# Returns-part1
res['part_01'] = dict()
res['part_01']['date_range'] = date_range()
res['part_01']['energy_consumption'] = ''.join(year_energy_new)
# part2
res['part_02'] = dict()
res['part_02']['date_range'] = date_range()
res['part_02']['energy_consumption'] = ''.join(year_thousand_energy_new)
res['part_02']['year_02'] = three_year_data[1]['年份']
res['part_02']['year_03'] = three_year_data[2]['年份']
res['part_02']['year01_on_year02'] = round((year_thousand_energy[1]/year_thousand_energy[0] - 1) * 100, 2)
res['part_02']['year02_on_year03'] = round((year_thousand_energy[2]/year_thousand_energy[1] - 1) * 100, 2)
res['part_02']['industry_indicators'] = None
res['part_02']['industry_indicators_average'] = None
res['part_02']['industry_indicators_median'] = None
res['part_02']['median_comparison'] = None
return res
result['part_01'] = calculate_three_year_data()['part_01']
result['part_02'] = calculate_three_year_data()['part_02']
return result
def environment_standard_coal(param):
"""
标准煤
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
income_data = param['公司当年收入(万元)']
three_year_data = sorted(param['环境问卷']['近三年公司数据'], key=operator.itemgetter('年份'), reverse=False)
# Returns
result = dict()
def date_range():
"""
评价周期年度区间
Parameters:
param dict 环境问卷
Returns:
result str 区间数据
"""
year_begin = three_year_data[0]['年份']
year_end = three_year_data[2]['年份']
res = year_begin + '-' + year_end
return res
def standard_coal(year_param, income):
"""
能耗折算标准煤
Parameters:
year_param dict 年度环境问卷数据
Returns:
result str 该年度二氧化碳排放总量
"""
total = list()
total.append(round(year_param['公司当年消耗的原煤(千克)'] * 0.7143, 2))
total.append(round(year_param['公司当年消耗的焦炭(千克)'] * 0.9714, 2))
total.append(round(year_param['公司当年消耗的原油(升)'] * 0.86 * 1.4286, 2))
total.append(round(year_param['公司当年消耗的燃料油(升)'] * 0.86 * 1.4286, 2))
total.append(round(year_param['公司当年消耗的汽油(升)'] * 0.73 * 1.4714, 2))
total.append(round(year_param['公司当年消耗的煤油(升)'] * 0.82 * 1.4714, 2))
total.append(round(year_param['公司当年消耗的柴油(升)'] * 0.85 * 1.4571, 2))
total.append(round(year_param['公司当年消耗的液化石油气(千克)'] * 1.7143, 2))
total.append(round(year_param['公司当年消耗的天然气(立方米)'] * 1.3300, 2))
total.append(round(year_param['公司当年消耗的煤气(立方米)'] * 0.5700, 2))
total.append(round(year_param['公司当年消耗的电(千瓦时)'] * 0.4040, 2))
amount = sum(total)
res = dict()
res['amount'] = amount
num = 2
while round(amount / income, num) == 0.0:
num += 1
res['money_amount'] = round(amount / income, num + 1)
return res
def calculate_three_year_data():
"""
计算三年数据并转为字符串
Parameters:
'-'
Returns:
result str 三年数据用顿号隔开
"""
res = dict()
year_standard_coal = list()
year_thousand_standard_coal = list()
for index in range(len(three_year_data)):
standard_coal_res = standard_coal(three_year_data[index], income_data[three_year_data[index]['年份']])
year_standard_coal.append(standard_coal_res['amount'])
year_thousand_standard_coal.append(standard_coal_res['money_amount'])
# 列表中元素类型float转str
year_standard_coal_new = [str(x) for x in year_standard_coal]
year_thousand_standard_coal_new = [str(x) for x in year_thousand_standard_coal]
# Returns-part1
res['part_01'] = dict()
res['part_01']['date_range'] = date_range()
res['part_01']['energy_consumption'] = ''.join(year_standard_coal_new)
# part2
res['part_02'] = dict()
res['part_02']['date_range'] = date_range()
res['part_02']['energy_consumption'] = ''.join(year_thousand_standard_coal_new)
return res
result['part_01'] = calculate_three_year_data()['part_01']
result['part_02'] = calculate_three_year_data()['part_02']
return result
def environment_consume_water(param):
"""
耗水
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
income_data = param['公司当年收入(万元)']
three_year_data = sorted(param['环境问卷']['近三年公司数据'], key=operator.itemgetter('年份'), reverse=False)
# Returns
result = dict()
def date_range():
"""
评价周期年度区间
Parameters:
param dict 环境问卷
Returns:
result str 区间数据
"""
year_begin = three_year_data[0]['年份']
year_end = three_year_data[2]['年份']
res = year_begin + '-' + year_end
return res
def calculate_three_year_data():
"""
计算三年数据并转为字符串
Parameters:
'-'
Returns:
result str 三年数据用顿号隔开
"""
res = dict()
year_consume_water = list()
year_thousand_consume_water = list()
for index in range(len(three_year_data)):
year_consume_water.append(three_year_data[index]['公司当年耗水(吨)'])
year_thousand_consume_water.append(round(three_year_data[index]['公司当年耗水(吨)'] / income_data[three_year_data[index]['年份']]))
# 列表中元素类型float转str
year_consume_water_new = [str(x) for x in year_consume_water]
year_thousand_consume_water_new = [str(x) for x in year_thousand_consume_water]
# Returns-part1
res['part_01'] = dict()
res['part_01']['date_range'] = date_range()
res['part_01']['consume_water'] = ''.join(year_consume_water_new)
# part2
res['part_02'] = dict()
res['part_02']['date_range'] = date_range()
res['part_02']['consume_water'] = ''.join(year_thousand_consume_water_new)
res['part_02']['year_02'] = three_year_data[1]['年份']
res['part_02']['year_03'] = three_year_data[2]['年份']
try:
res['part_02']['year01_on_year02'] = round((year_thousand_consume_water[1]/year_thousand_consume_water[0]-1) * 100, 2)
res['part_02']['year02_on_year03'] = round((year_thousand_consume_water[2]/year_thousand_consume_water[1]-1) * 100, 2)
except ZeroDivisionError:
res['part_02']['year01_on_year02'] = '0'
res['part_02']['year02_on_year03'] = '0'
res['part_02']['industry_indicators'] = None
res['part_02']['industry_indicators_average'] = None
res['part_02']['industry_indicators_median'] = None
res['part_02']['median_comparison'] = None
return res
result['part_01'] = calculate_three_year_data()['part_01']
result['part_02'] = calculate_three_year_data()['part_02']
return result
def environment_green_business(param):
"""
绿色业务收入
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
income_data = param['公司当年收入(万元)']
three_year_data = sorted(param['环境问卷']['近三年公司数据'], key=operator.itemgetter('年份'), reverse=False)
# Returns
result = dict()
if three_year_data[2]['公司当年绿色业务收入(万元)——包括不限于清洁能源、清洁交通、绿色建筑、清洁技术等'] == 0:
result['describe'] = False
else:
year = three_year_data[2]['年份']
share = round(three_year_data[2]['公司当年绿色业务收入(万元)——包括不限于清洁能源、清洁交通、绿色建筑、清洁技术等'] / income_data[three_year_data[2]['年份']], 2)
result['describe'] = '绿色业务收入占比(%)——公司在{year_03}年绿色业务收入占比为{revenue_share},这提升了公司的环境得分。'.format(year_03=year, revenue_share=share)
return result
def environment_punish(param):
"""
处罚
Parameters:
param dict 数据库数据
Returns:
result dict 清洗好的数据
"""
# Params
data = param['环境问卷']
# Returns
result = dict()
if data['其他类型问卷'][4][0] == '':
result['describe'] = False
else:
result['describe'] = '处罚——公司在近三年因环境问题受到监管处罚,这影响了公司的环境得分。'
return result