# -*- coding: utf-8 -*-
"""
Spyder 编辑器

这是一个临时脚本文件。
"""
import os

import pandas as pd

pd.set_option('chained_assignment', None)

select_cols = ['遥测ID号', '风场', '风场几期', '风机号', '测点', '标准化中文', '转发顺序号', 'en_name']

origin_col_map = {
    'wind_turbine_number': '风机编号', 'wind_turbine_name': '风机原始名称', 'time_stamp': '时间戳',
    'active_power': '有功功率', 'rotor_speed': '风轮转速', 'generator_speed': '发电机转速', 'wind_velocity': '风速',
    'pitch_angle_blade_1': '桨距角1', 'pitch_angle_blade_2': '桨距角2', 'pitch_angle_blade_3': '桨距角3',
    'cabin_position': '机舱位置', 'true_wind_direction': '绝对风向', 'yaw_error1': '对风角度',
    'set_value_of_active_power': '有功功率设定值', 'gearbox_oil_temperature': '齿轮箱油温',
    'generatordrive_end_bearing_temperature': '发电机驱动端轴承温度',
    'generatornon_drive_end_bearing_temperature': '发电机非驱动端轴承温度', 'cabin_temperature': '机舱内温度',
    'twisted_cable_angle': '扭缆角度', 'front_back_vibration_of_the_cabin': '机舱前后振动',
    'side_to_side_vibration_of_the_cabin': '机舱左右振动', 'actual_torque': '实际力矩', 'given_torque': '给定力矩',
    'clockwise_yaw_count': '顺时针偏航次数', 'counterclockwise_yaw_count': '逆时针偏航次数', 'unusable': '不可利用',
    'power_curve_available': '功率曲线可用', 'required_gearbox_speed': '齿轮箱转速',
    'inverter_speed_master_control': '变频器转速(主控)', 'outside_cabin_temperature': '环境温度',
    'main_bearing_temperature': '主轴承轴承温度',
    'gearbox_high_speed_shaft_bearing_temperature': '齿轮箱高速轴轴承温度',
    'gearboxmedium_speed_shaftbearing_temperature': '齿轮箱中速轴轴承温度',
    'gearbox_low_speed_shaft_bearing_temperature': '齿轮箱低速轴轴承温度',
    'generator_winding1_temperature': '发电机绕组1温度', 'generator_winding2_temperature': '发电机绕组2温度',
    'generator_winding3_temperature': '发电机绕组3温度', 'wind_turbine_status': '风机状态1',
    'wind_turbine_status2': '风机状态2', 'turbulence_intensity': '湍流强度'
}


def youyu_measurepoint(df, name):
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[0][0:4])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[1][0:3])
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('_')[1].split('-')[1][3:])
    show_measurepoint(df, name + '测点')
    col_mapping = {
        "3#桨叶片角度": "pitch_angle_blade_3",
        "机舱侧向振动(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱轴向振动(已滤波)": "front_back_vibration_of_the_cabin",
        "机舱角度": "cabin_position",
        "扭缆角度": "twisted_cable_angle",
        "发电机有功功率": "active_power",
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "绕组温度1": "generator_winding1_temperature",
        "绕组温度2": "generator_winding2_temperature",
        "绕组温度3": "generator_winding3_temperature",
        "舱内温度": "cabin_temperature",
        "舱外温度": "outside_cabin_temperature",
        "风向": "true_wind_direction",
        "风速": "wind_velocity",
        "机舱中轴线与风向夹角": "yaw_error1",
        "1#桨叶片角度": "pitch_angle_blade_1",
        "2#桨叶片角度": "pitch_angle_blade_2",
        "发电机转速": "generator_speed",
        "主轴转速": "rotor_speed",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "齿轮箱油池温度": "gearbox_oil_temperature",
        "齿轮箱中速轴非驱动端轴承温度": "gearboxmedium_speed_shaftbearing_temperature",
        "主轴承外圈温度": "main_bearing_temperature",
    }

    df['en_name'] = df['测点'].map(col_mapping)
    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def pinglu_measurepoint(df, name):
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[0])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[2].split('-')[1][0:2])
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('_')[1].split('-')[1][2:])
    show_measurepoint(df, name + '测点')
    col_mapping = {
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "发电机转速": "generator_speed",
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "2#桨叶片角度": "pitch_angle_blade_2",
        "主轴承外圈温度": "main_bearing_temperature",
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "发电机有功功率": "active_power",
        "主轴转速": "rotor_speed",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "1#桨叶片角度": "pitch_angle_blade_1",
        "舱外温度": "outside_cabin_temperature",
        "舱内温度": "cabin_temperature",
        "风向": "true_wind_direction",
        "机舱中轴线与风向夹角": "yaw_error1",
        "机舱角度": "cabin_position",
        "扭缆角度": "twisted_cable_angle",
        "3#桨叶片角度": "pitch_angle_blade_3",
        "机舱轴向振动(已滤波)": "front_back_vibration_of_the_cabin",
        "风速": "wind_velocity",
        "机舱侧向振动(已滤波)": "side_to_side_vibration_of_the_cabin",
        "齿轮箱高速轴费非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",  # 与“齿轮箱高速轴非驱动端轴承温度”相同
        "机舱角度（位置）": "cabin_position",
        "机舱侧向振动值(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱轴向振动值(已滤波)": "front_back_vibration_of_the_cabin",
        "机舱与风向夹角": "yaw_error1",
        "1#桨叶片角度(桨距角)": "pitch_angle_blade_1",
        "2#桨叶片角度(桨距角)": "pitch_angle_blade_2",
        "3#桨叶片角度(桨距角)": "pitch_angle_blade_3",
        "齿轮箱高速轴驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",  # 根据规则，齿轮箱高速轴不区分驱动端和非驱动端
        "齿轮箱中间轴非驱动端轴承温度": "gearboxmedium_speed_shaftbearing_temperature",
    }

    df['en_name'] = df['测点'].map(col_mapping)
    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def ruicheng_measurepoint(df, name):
    # 根据芮城数据格式调整字段提取逻辑
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1][0:4])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    # 假设风机号在第三个分割段中
    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1][6:9])
    # 测点提取可能需要调整切片位置
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('_')[1].split('-')[1][3:])
    show_measurepoint(df, name + '测点')
    col_mapping = {
        "1#桨叶片角度": "pitch_angle_blade_1",
        "2#桨叶片角度": "pitch_angle_blade_2",
        "3#桨叶片角度": "pitch_angle_blade_3",
        "舱内温度": "cabin_temperature",
        "舱外温度": "outside_cabin_temperature",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "齿轮箱油池温度": "gearbox_oil_temperature",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "发电机有功功率": "active_power",
        "发电机转速": "generator_speed",
        "风速": "wind_velocity",
        "风向": "true_wind_direction",
        "机舱侧向振动(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱角度": "cabin_position",
        "机舱中轴线与风向夹角": "yaw_error1",
        "机舱轴向振动(已滤波)": "front_back_vibration_of_the_cabin",
        "扭缆角度": "twisted_cable_angle",
        "主轴承外圈温度": "main_bearing_temperature",
        "主轴转速": "rotor_speed",
        "机舱与风向夹角": "yaw_error1",
        "机舱角度（位置）": "cabin_position",
        "1#桨叶片角度(桨距角)": "pitch_angle_blade_1",
        "2#桨叶片角度(桨距角)": "pitch_angle_blade_2",
        "3#桨叶片角度(桨距角)": "pitch_angle_blade_3",
    }

    df['en_name'] = df['测点'].map(col_mapping)
    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def yuxian_measurepoint(df, name):
    # 根据孟县数据格式调整字段提取逻辑
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[0][0:4])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    # 假设风机号在第三个分割段中
    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('-')[1][0:3])
    # 测点提取可能需要调整切片位置
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('-')[1][3:])

    show_measurepoint(df, name + '测点')
    col_mapping = {
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "机舱侧向振动(已滤波)": "side_to_side_vibration_of_the_cabin",
        "风速": "wind_velocity",
        "机舱中轴线与风向夹角": "yaw_error1",
        "1#桨叶片角度": "pitch_angle_blade_1",
        "2#桨叶片角度": "pitch_angle_blade_2",
        "发电机有功功率": "active_power",
        "发电机转速": "generator_speed",
        "风向": "true_wind_direction",
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "机舱轴向振动(已滤波)": "front_back_vibration_of_the_cabin",
        "机舱角度": "cabin_position",
        "扭揽角度": "twisted_cable_angle",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "主轴转速": "rotor_speed",
        "齿轮箱中速轴非驱动端轴承温度": "gearboxmedium_speed_shaftbearing_temperature",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "齿轮箱油池温度": "gearbox_oil_temperature",
        "主轴承外圈温度": "main_bearing_temperature",
        "舱内温度": "cabin_temperature",
        "舱外温度": "outside_cabin_temperature",
        "3#桨叶片角度": "pitch_angle_blade_3",
        "扭缆角度": "twisted_cable_angle",
        "齿轮箱中间轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",  # 与“齿轮箱中速轴非驱动端轴承温度”相同
        "主轴(风轮)转速": "rotor_speed",
        "1#桨叶片角度(桨距角)": "pitch_angle_blade_1",
        "2#桨叶片角度(桨距角)": "pitch_angle_blade_2",
        "3#桨叶片角度(桨距角)": "pitch_angle_blade_3",
        "机舱角度（位置）": "cabin_position",
        "机舱侧向振动值(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱轴向振动值(已滤波)": "front_back_vibration_of_the_cabin",
    }

    df['en_name'] = df['测点'].map(col_mapping)
    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def gujiao_measurepoint(df, name):
    # 古交数据格式处理
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[0][0:4])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    # 假设风机号在第三个分割段中
    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('-')[1][0:3])
    # 测点提取可能需要调整切片位置
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('-')[1][3:])

    show_measurepoint(df, name + '测点')
    col_mapping = {
        "机舱与风向夹角": "yaw_error1",
        "1#桨叶片角度(桨距角)": "pitch_angle_blade_1",
        "2#桨叶片角度(桨距角)": "pitch_angle_blade_2",
        "3#桨叶片角度(桨距角)": "pitch_angle_blade_3",
        "主轴转速": "rotor_speed",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "齿轮箱油池温度": "gearbox_oil_temperature",
        "主轴承外圈温度": "main_bearing_temperature",
        "发电机转速": "generator_speed",
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "舱内温度": "cabin_temperature",
        "舱外温度": "outside_cabin_temperature",
        "风向": "true_wind_direction",
        "机舱角度（位置）": "cabin_position",
        "偏航角度(扭缆角度）": "twisted_cable_angle",
        # "桨叶片角度(桨距角)": ["pitch_angle_blade_1", "pitch_angle_blade_2", "pitch_angle_blade_3"],  # 根据具体桨叶选择
        "机舱侧向振动值(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱轴向振动值(已滤波)": "front_back_vibration_of_the_cabin",
        "风速": "wind_velocity",
        "发电机有功功率": "active_power",
    }

    df['en_name'] = df['测点'].map(col_mapping)

    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def shilou_measurepoint(df, name):
    # 石楼数据格式处理
    df['风场几期'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('_')[1].split('-')[0][0:4])
    df['风场'] = df['风场几期'].apply(lambda x: x[0:len(name)])

    # 假设风机号在第三个分割段中
    df['风机号'] = df['遥测ID号'].apply(lambda x: x.split(' ')[1].split('-')[1][0:3])
    # 测点提取可能需要调整切片位置
    df['测点'] = df['遥测ID号'].apply(
        lambda x: ''.join(x.replace('遥测值', '').strip().split(' ')[1:]).split('-')[1][3:])
    show_measurepoint(df, name + '测点')
    col_mapping = {
        "舱内温度": "cabin_temperature",
        "舱外温度": "outside_cabin_temperature",
        "偏航角度(扭缆角度）": "twisted_cable_angle",
        "风向": "true_wind_direction",
        "风速": "wind_velocity",
        "机舱中轴线与风向夹角": "yaw_error1",
        "1#桨叶片角度": "pitch_angle_blade_1",
        "2#桨叶片角度": "pitch_angle_blade_2",
        "3#桨叶片角度": "pitch_angle_blade_3",
        "主轴转速": "rotor_speed",
        "机舱轴向振动(已滤波)": "front_back_vibration_of_the_cabin",
        "发电机有功功率": "active_power",
        "发电机转速": "generator_speed",
        "机舱与风向夹角": "yaw_error1",
        "1#桨叶片角度(桨距角)": "pitch_angle_blade_1",
        "2#桨叶片角度(桨距角)": "pitch_angle_blade_2",
        "3#桨叶片角度(桨距角)": "pitch_angle_blade_3",
        "齿轮箱油池温度": "gearbox_oil_temperature",
        "主轴后轴承温度": "main_bearing_temperature",  # 根据规则，主轴后轴承温度对应 main_bearing_temperature
        "发电机驱动端轴承温度": "generatordrive_end_bearing_temperature",
        "发电机非驱动端轴承温度": "generatornon_drive_end_bearing_temperature",
        "齿轮箱高速轴非驱动端轴承温度": "gearbox_high_speed_shaft_bearing_temperature",
        "发电机定子U相线圈温度": "generator_winding1_temperature",
        "发电机定子V相线圈温度": "generator_winding2_temperature",
        "发电机定子W相线圈温度": "generator_winding3_temperature",
        "机舱侧向振动值(已滤波)": "side_to_side_vibration_of_the_cabin",
        "机舱轴向振动值(已滤波)": "front_back_vibration_of_the_cabin",
        "齿轮箱中速轴非驱动端轴承温度": "gearboxmedium_speed_shaftbearing_temperature",
        "机舱角度（位置）": "cabin_position",
    }

    df['en_name'] = df['测点'].map(col_mapping)

    print(df.groupby('en_name').count())

    df.sort_values(by='转发顺序号', inplace=True)
    save_df(df, name + '测点')
    return df


def show_measurepoint(df, name):
    print(f'--------{name}-----------')
    for cn_name in df['测点'].unique():
        print(f"{cn_name}:'',")
    print('-------------------')


def save_df(df, name):
    df['遥测ID号'] = df['遥测ID号'].apply(lambda x: x.replace('遥测定义表 ', '').replace('遥测值', '').strip())
    df['标准化中文'] = df['en_name'].map(origin_col_map)

    df.to_csv(r'C:\Users\wzl\Desktop\中广核104测点\2404' + os.sep + str(name) + '.csv',
              columns=select_cols,
              index=False, encoding='utf8')


if __name__ == '__main__':
    df = pd.read_csv(r"D:\data\tmp\2404ZF.csv", encoding='gbk')

    # 添加右玉处理
    youyu_df = youyu_measurepoint(df[df['遥测ID号'].str.contains('右玉')], '右玉')

    # 添加平陆处理
    pinglu_df = pinglu_measurepoint(df[df['遥测ID号'].str.contains('平陆')], '平陆')

    # 添加芮城处理
    ruicheng_df = ruicheng_measurepoint(df[df['遥测ID号'].str.contains('芮城')], '芮城')

    # 添加盂县处理
    yuxian_df = yuxian_measurepoint(df[df['遥测ID号'].str.contains('盂县')], '盂县')

    # 添加古交处理
    gujiao_df = gujiao_measurepoint(df[df['遥测ID号'].str.contains('古交')], '古交')

    # 添加石楼处理
    shilou_df = shilou_measurepoint(df[df['遥测ID号'].str.contains('石楼')], '石楼')

    result_df = pd.concat([youyu_df, pinglu_df, ruicheng_df, yuxian_df, gujiao_df, shilou_df])

    save_df(result_df, '2404测点')
