2018-10-29漁船數(shù)據(jù)的兩種信號報表及圖片輸出

-- coding: utf-8 --

import os
import datetime
import pymysql
import matplotlib.pyplot as plt
from pylab import mpl
import time
import xlwt
import xlrd
import threading
import pandas as pd
import glob
from matplotlib.font_manager import FontProperties
import sys
import platform
from shutil import copy
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
from DBUtils.PooledDB import PooledDB
from future.types import no

def parse_datetime(s):
try:
if s.find(' ') != -1:
return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
else:
return datetime.datetime.strptime(s, "%Y-%m-%d")
except:
print ("string '%s' is not a valid date or datetime")
def datetime_toString(dt):
return dt.strftime("%Y-%m-%d %H:%M:%S")

def datetime_toTimestamp(dt):
return time.mktime(dt.timetuple())

def timestamp_toString(sp):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sp))

POOL = PooledDB(
creator=pymysql, # 使用鏈接數(shù)據(jù)庫的模塊
maxconnections=11, # 連接池允許的最大連接數(shù)倦畅,0和None表示不限制連接數(shù)
mincached=3, # 初始化時鸭丛,鏈接池中至少創(chuàng)建的空閑的鏈接,0表示不創(chuàng)建
maxcached=6, # 鏈接池中最多閑置的鏈接凯沪,0和None不限制
maxshared=4, # 鏈接池中最多共享的鏈接數(shù)量鸠儿,0和None表示全部共享献丑。PS: 無用,因為pymysql和MySQLdb等模塊的 threadsafety都為1诽嘉,所有值無論設置為多少蔚出,_maxcached永遠為0,所以永遠是所有鏈接都共享虫腋。
blocking=True, # 連接池中如果沒有可用連接后骄酗,是否阻塞等待。True岔乔,等待酥筝;False,不等待然后報錯
maxusage=None, # 一個鏈接最多被重復使用的次數(shù)雏门,None表示無限制
setsession=[], # 開始會話前執(zhí)行的命令列表嘿歌。如:["set datestyle to ...", "set time zone ..."]
ping=0,
# ping MySQL服務端,檢查是否服務可用茁影。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='',
port=,
user='',
password='',
database='',
charset='utf8'
)

def execute_sql(sql_cmd):
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute(sql_cmd)
results = cursor.fetchall()
conn.close()

db = pymysql.connect(host='61.164.208.174',user='root',passwd='root',db='db_rcld_zj_statistics',port=8116,charset='utf8')

cursor = db.cursor()

cursor.execute(sql_cmd)

results = cursor.fetchall()

db.close()

return results

通過日期區(qū)間和漁船設備號查找數(shù)據(jù)

def get_signal_info_by_device_no(from_date, to_date,device_no):
print ("Partition ship data by device_no")
process_day = from_date
signal_change_points= []
data_time_temp1 = 0
data_time_temp2 = 0
signal_change_point_temp1 = 0
signal_change_point_temp2 = 0
pre_temp_dts =[]
temp_dts =[]
GPRS_TIME = 0
LBS_TIME = 0
GPRS_loss_num = 0
GPRS_loss_point = []
GPRS_fact_num = 0
LBS_fact_num = 0
LBS_loss_num =0
LBS_loss_point = []
singnal_strength_sum = 0
cpu_tempreture_sum = 0
libv_count = 0
libv_sum = 0.0
pvbv_sum = 0.0
pvbv_count = 0
flag = 0 #用于日期遞增
flag1 = 0 #判斷信號頻率轉換的標志位
flag2 = 1 #判斷GPRS轉為北斗信號后的頻率為2分鐘一次還是5分鐘一次的標志位
flag3 = 1 #判斷第12標志位為1時出現(xiàn)10分鐘一次的GPRS信號
while process_day <= to_date:
table_name = process_day.strftime("t_acq_data_%Y%m%d")
get_ship_data_by_date_cmd = ("select ALARM_STATUS,ACQ_TIME,DATA_TYPE,SIGNAL_STRENGTH,
CPU_TEMPRETURE,LIBV,PVBV from %s where DEVICE_NO = %s" % (table_name,device_no))
pre_temp_dts = execute_sql(get_ship_data_by_date_cmd)
if pre_temp_dts == () or pre_temp_dts == None:
print('--------------device_no為'+device_no+'在'+str(process_day.date())+'這天沒有數(shù)據(jù)')
else:
for pre_temp_dt in pre_temp_dts:
temp_dts.append({'ALARM_STATUS': str(pre_temp_dt[0]), 'ACQ_TIME': pre_temp_dt[1],
'DATA_TYPE': pre_temp_dt[2],'SIGNAL_STRENGTH': pre_temp_dt[3],'CPU_TEMPRETURE': pre_temp_dt[4],
'LIBV': pre_temp_dt[5],'PVBV': pre_temp_dt[6]})
flag +=1
process_day += datetime.timedelta(1)
if (temp_dts) == []:
print('--------------device_no為'+device_no+'在這幾天都沒有數(shù)據(jù)')
else:

    if (int(temp_dts[0].get('ALARM_STATUS'),2) & int(8192)):
        data_type_begin = 2
        flag1 = 4
    else:
        data_type_begin = 1  
    data_time_temp1 = datetime_toTimestamp(temp_dts[0].get('ACQ_TIME'))
    signal_change_point_temp1 = data_time_temp1
    signal_change_points.append(timestamp_toString(data_time_temp1))
    for temp_dt in temp_dts:
        
        libv_count += 1
        libv_sum += temp_dt.get('LIBV')
        
        times = str(temp_dt.get('ACQ_TIME'))
        seconds = (int(times[11])*10+int(times[12]))*3600+(int(times[14])*10+\
        int(times[15]))*60+(int(times[17])*10+int(times[18]))
        if  seconds >= (6*3600) and seconds <= (18*3600):
            pvbv_count += 1
            pvbv_sum += temp_dt.get('PVBV')

singnal_strength_sum += temp_dt.get('SIGNAL_STRENGTH')

cpu_tempreture_sum += temp_dt.get('CPU_TEMPRETURE')

        data_time_temp2 = datetime_toTimestamp(temp_dt.get('ACQ_TIME'))#下一個點的時間以秒數(shù)計算
        if (2 if int(temp_dt.get('ALARM_STATUS'),2) & int(8192) else 1) == 1:#判斷是否GPRS信號
            GPRS_fact_num += 1
            if flag1 == 4:
                flag2 = 1
                signal_change_points.append(timestamp_toString(data_time_temp2))
                signal_change_point_temp2 = data_time_temp2
                LBS_TIME += signal_change_point_temp2 - signal_change_point_temp1
                signal_change_point_temp1 = signal_change_point_temp2
                if (int(temp_dt.get('ALARM_STATUS'),2) & int(32)):#5分鐘一次
                    flag1 =1
                elif (int(temp_dt.get('ALARM_STATUS'),2) & int(2048)):#10分鐘一次
                    flag1 =2  
                else:#2分鐘一次
                    flag1 =3
            elif (int(temp_dt.get('ALARM_STATUS'),2) & int(32)):#5分鐘一次
                if (flag1 == 1) and (data_time_temp2-data_time_temp1) > (5*60+60):
                    GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/5)-1)
                    GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
                flag1 =1
            elif (int(temp_dt.get('ALARM_STATUS'),2) & int(2048)):#10分鐘一次
                if (flag1 == 2) and (flag3 == 1) and (data_time_temp2-data_time_temp1) < (10*60+60) and (data_time_temp2-data_time_temp1) > (10*60-60):
                    print('--------------------警告:設備號為'+device_no+'第12標志位為1時出現(xiàn)10分鐘一次的GPRS信號----------------------------')
                    flag3 = 2
                if (flag1 == 2) and (data_time_temp2-data_time_temp1) > (10*60+120):
                    GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/10)-1)
                    GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
                flag1 =2  
            else:#2分鐘一次
                if (flag1 == 3) and (data_time_temp2-data_time_temp1) > (2*60+40):
                    GPRS_loss_num += (round((data_time_temp2-data_time_temp1)/60/2)-1)
                    GPRS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
                flag1 =3           
        elif (2 if int(temp_dt.get('ALARM_STATUS'),2) & int(8192) else 1) == 2:#判斷是否北斗信號    
            LBS_fact_num += 1 
            if not (flag1 == 4):
                signal_change_points.append(timestamp_toString(data_time_temp2))
                signal_change_point_temp2 = data_time_temp2
                GPRS_TIME += (signal_change_point_temp2 - signal_change_point_temp1)
                signal_change_point_temp1 = signal_change_point_temp2
            else:
                if flag2 == 1:
                    if (data_time_temp2-data_time_temp1) > (5*60-40) and (data_time_temp2-data_time_temp1) < (5*60+40):
                        flag2 = 2#標志2分鐘一次變?yōu)?分鐘一次
                    else:
                        if (data_time_temp2-data_time_temp1) > (2*60+40):
                            LBS_loss_num += (round((data_time_temp2-data_time_temp1)/60/2)-1) 
                            LBS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
                elif flag2 == 2 and (data_time_temp2-data_time_temp1) > (5*60+60):
                    LBS_loss_num += (round((data_time_temp2-data_time_temp1)/60/5)-1)
                    LBS_loss_point.append(datetime_toString(temp_dt.get('ACQ_TIME')))
            flag1 = 4 
        elif int(temp_dt.get('ALARM_STATUS'),2) & int(49152):
            print('-------------------存在報警信息-------------------------------') 
        data_time_temp1=data_time_temp2
    signal_change_point_temp2 = datetime_toTimestamp(temp_dts[-1].get('ACQ_TIME'))
    signal_change_points.append(timestamp_toString(signal_change_point_temp2))
    if not flag1 == 4:
        GPRS_TIME += signal_change_point_temp2-signal_change_point_temp1
    else:
        LBS_TIME += signal_change_point_temp2-signal_change_point_temp1         

print('GPRS_fact_num:'+ str(GPRS_fact_num))
print('GPRS_loss_num:'+ str(GPRS_loss_num))
print('LBS_fact_num:'+ str(LBS_fact_num))
print('LBS_loss_num:'+ str(LBS_loss_num))
print('GPRS_TIME:'+ str(GPRS_TIME))
print('LBS_TIME:'+ str(LBS_TIME))
print('signal_change_points:'+ str(signal_change_points))
print('GPRS_loss_point:'+ str(GPRS_loss_point))
print('LBS_loss_point:'+ str(LBS_loss_point))
if GPRS_fact_num == 0:
    gprs_success = None
else:
    gprs_success =  GPRS_fact_num/(GPRS_fact_num + GPRS_loss_num)
if LBS_fact_num == 0:
    lbs_success = None
else:
    lbs_success =  LBS_fact_num/(LBS_fact_num + LBS_loss_num)
if GPRS_TIME + LBS_TIME == 0:
    gprs_time_percent = None
    lbs_time_percent = None
else:
    gprs_time_percent = GPRS_TIME/(GPRS_TIME + LBS_TIME)
    lbs_time_percent = LBS_TIME/(GPRS_TIME + LBS_TIME)
if libv_count == 0:
    libv_average = None
else:
    libv_average = libv_sum/libv_count
if pvbv_count == 0:
    pvbv_average = None
else:
    pvbv_average = pvbv_sum/pvbv_count

return   gprs_success,lbs_success,gprs_time_percent,lbs_time_percent,libv_average, pvbv_average

def get_device_nos(dev_type):
get_device_nos_by_date_cmd = ("select distinct(DEVICE_NO) from t_device where DEVICE_TYPE = %s" % (dev_type))
temp_device_nos = execute_sql(get_device_nos_by_date_cmd)
device_nos=[]
for temp_device_no in temp_device_nos:
device_nos.append(temp_device_no[0])
return device_nos

def chunks(device_nos, n):
for i in range(0, len(device_nos), n):
yield device_nos[i:i + n]

def get_excel(device_nos,num):

path = '.'
os.chdir(path)
tmp_path = './tmp'
if not os.path.exists(tmp_path):  
    os.mkdir(tmp_path)
outexcel_path = os.path.join(tmp_path, '設備數(shù)據(jù)報表%s.xls' % (num))

flag_excel = 1 
workbook = xlwt.Workbook(encoding='utf-8')
booksheet = workbook.add_sheet('Sheet 1', cell_overwrite_ok=True)
c = ['設備NO', '統(tǒng)計數(shù)據(jù)時間區(qū)間','GPRS成功率', 'GPRS占比', '北斗成功率','北斗占比','平均鋰電池電壓','平均光伏電池電壓','設備代數(shù)']
#用于初步處理后的數(shù)據(jù)存儲
data_toexcel = []
for i in range(9):
    booksheet.write(0, i, c[i])
for device_no in device_nos:
    print('---------------------------------------------------------------------------------------------------------')
    print('ship which is now handled is device_no:'+ str(device_no))
    gprs_success,lbs_success,gprs_time_percent,lbs_time_percent,libv_average,pvbv_average = get_signal_info_by_device_no(from_date, to_date,device_no)
    data_toexcel.append({"DEVICE_NO": device_no, "COUNT_TIME_INTERVAL": "%s-%s" % (from_date,to_date), "GPRS_SUCCESS": gprs_success, "GPRS_TIME_PERCENT": gprs_time_percent,\
              "LBS_SUCCESS": lbs_success,"LBS_TIME_PERCENT": lbs_time_percent, "DEVICE_TYPE": '三代', "LIBV_AVERAGE": libv_average,"PVBV_AVERAGE": pvbv_average}) 
    booksheet.write(flag_excel, 0, data_toexcel[flag_excel-1].get('DEVICE_NO'))
    booksheet.write(flag_excel, 1, data_toexcel[flag_excel-1].get('COUNT_TIME_INTERVAL'))
    booksheet.write(flag_excel, 2, data_toexcel[flag_excel-1].get('GPRS_SUCCESS'))
    booksheet.write(flag_excel, 3, data_toexcel[flag_excel-1].get('GPRS_TIME_PERCENT'))
    booksheet.write(flag_excel, 4, data_toexcel[flag_excel-1].get('LBS_SUCCESS'))
    booksheet.write(flag_excel, 5, data_toexcel[flag_excel-1].get('LBS_TIME_PERCENT'))
    booksheet.write(flag_excel, 6, data_toexcel[flag_excel-1].get('LIBV_AVERAGE'))
    booksheet.write(flag_excel, 7, data_toexcel[flag_excel-1].get('PVBV_AVERAGE'))
    booksheet.write(flag_excel, 8, data_toexcel[flag_excel-1].get('DEVICE_TYPE'))
    flag_excel += 1
    print('-----------------------flag_excel:'+str(flag_excel))

    workbook.save(outexcel_path)    

def excels_toOne_excel(dest_path=None, path= '.'):
filearray=[]
filelocation="./tmp/"
for filename in glob.glob(filelocation+"*.xls"):
filearray.append(filename)
res=pd.read_excel(filearray[0])
for i in range(1,len(filearray)):
A=pd.read_excel(filearray[i])
res=pd.concat([res,A],ignore_index=True)
print(res.index)
os.chdir(path)
if dest_path is None:
dest_path = os.path.join(path, '%s_%s' % (from_date.date(), to_date.date()))
if not os.path.exists(dest_path):
os.mkdir(dest_path)
outfn = os.path.join(dest_path, u"漁船設備報表.xlsx")
writer = pd.ExcelWriter(outfn)
res.to_excel(writer,'sheet1')
writer.save()
"""
@summary:
根據(jù)平臺類型處理字體庫
linux: 復制字體庫到系統(tǒng)字體目錄宙帝,并手動載入使用
windows: 設置matplot的字體參數(shù)即可
"""
def prepare_png_font():
font = None
if platform.system() == 'Windows':
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認字體
else:
fontFile = '/usr/share/fonts/SimHei.ttf'
if not os.path.exists(fontFile) and os.path.exists('./SimHei.ttf'):
copy('./SimHei.ttf', fontFile)
font = FontProperties(fname=fontFile,size=17)

return font

def draw_signal(dev_type, from_date, to_date,dest_path=None,path='.'):
fname = os.path.join(path, './%s_%s/漁船設備報表.xlsx' % (from_date.date(), to_date.date()))
book = xlrd.open_workbook(fname)
try:
sheet=book.sheet_by_name("sheet1")
except:
print ("在文件%s中沒有找到sheet1,讀取文件數(shù)據(jù)失敗,要不你換換表格的名字募闲?" %fname)
nrows = sheet.nrows

gprs_success_count_100=0
gprs_success_count_90=0
gprs_success_count_80=0
gprs_success_count_70=0
gprs_success_count_60=0
gprs_success_count_50=0
gprs_success_count_40=0
gprs_success_count_30=0
gprs_success_count_20=0
gprs_success_count_10=0 
gprs_success_count_0=0 
gprs_success = 0.0
gprs_time_count_100=0
gprs_time_count_90=0
gprs_time_count_80=0
gprs_time_count_70=0
gprs_time_count_60=0
gprs_time_count_50=0
gprs_time_count_40=0
gprs_time_count_30=0
gprs_time_count_20=0
gprs_time_count_10=0 
gprs_time_count_0=0 
gprs_time_percent = 0.0

for j in range(1,nrows-1):
    if not sheet.cell(j,3).value == '':
        gprs_success = sheet.cell(j,3).value
    else:
        gprs_success = ''
    if not gprs_success == '':
        if(gprs_success == 1.0):
            gprs_success_count_100+=1
        if(gprs_success < 1.0 and gprs_success >= 0.9):
            gprs_success_count_90+=1
        if(gprs_success < 0.9 and gprs_success>=0.8):
            gprs_success_count_80+=1
        if(gprs_success< 0.8 and gprs_success>=0.7):
            gprs_success_count_70+=1
        if(gprs_success<0.7 and gprs_success>=0.6):
            gprs_success_count_60+=1
        if(gprs_success< 0.6 and gprs_success>=0.5):
            gprs_success_count_50+=1
        if(gprs_success<0.5 and gprs_success>= 0.4):
            gprs_success_count_40+=1
        if(gprs_success<0.4 and gprs_success>=0.3):
            gprs_success_count_30+=1
        if(gprs_success<0.3 and gprs_success>=0.2):
            gprs_success_count_20+=1
        if(gprs_success<0.2 and gprs_success>=0.1):
            gprs_success_count_10+=1
        if(gprs_success<0.1):
            gprs_success_count_0+=1
    if not sheet.cell(j,4).value == '':
        gprs_time_percent = sheet.cell(j,4).value
    else:
        gprs_time_percent = ''
    if not gprs_time_percent == '':
        if(gprs_time_percent == 1.0):
            gprs_time_count_100+=1
        if(gprs_time_percent < 1.0 and gprs_time_percent >= 0.9):
            gprs_time_count_90+=1
        if(gprs_time_percent < 0.9 and gprs_time_percent>=0.8):
            gprs_time_count_80+=1
        if(gprs_time_percent< 0.8 and gprs_time_percent>=0.7):
            gprs_time_count_70+=1
        if(gprs_time_percent<0.7 and gprs_time_percent>=0.6):
            gprs_time_count_60+=1
        if(gprs_time_percent< 0.6 and gprs_time_percent>=0.5):
            gprs_time_count_50+=1
        if(gprs_time_percent<0.5 and gprs_time_percent>= 0.4):
            gprs_time_count_40+=1
        if(gprs_time_percent<0.4 and gprs_time_percent>=0.3):
            gprs_time_count_30+=1
        if(gprs_time_percent<0.3 and gprs_time_percent>=0.2):
            gprs_time_count_20+=1
        if(gprs_time_percent<0.2 and gprs_time_percent>=0.1):
            gprs_time_count_10+=1
        if(gprs_time_percent<0.1):
            gprs_time_count_0+=1
gprs_name_list = ['100%','90%','80%','70%','60%','50%','40%','30%','20%','10%','<10%']
gprs_num_list = [gprs_success_count_100,gprs_success_count_90,gprs_success_count_80,gprs_success_count_70,gprs_success_count_60,gprs_success_count_50,gprs_success_count_40,gprs_success_count_30,gprs_success_count_20,gprs_success_count_10,gprs_success_count_0]
gprs_num_list1 = [gprs_time_count_100,gprs_time_count_90,gprs_time_count_80,gprs_time_count_70,gprs_time_count_60,gprs_time_count_50,gprs_time_count_40,gprs_time_count_30,gprs_time_count_20,gprs_time_count_10,gprs_time_count_0]
x =list(range(len(gprs_num_list)))
total_width, n = 0.8, 2
width = total_width / n 

myfont = matplotlib.font_manager.FontProperties(fname=r'C:/Windows/Fonts/simsun.ttc',size=14) # 這一行

mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默認字體

kwargs = {}
font = prepare_png_font()
if font:
    kwargs['fontproperties'] = font
#指定輸出圖片的尺寸
plt.figure(figsize=(14, 7))
# 設置標題
plt.title(u"漁船GPRS信號統(tǒng)計    設備類型:%s 時間段:%s ~ %s" % (dev_type,from_date.date(), to_date.date()), **kwargs)
b1 = plt.bar(x, gprs_num_list, width=width,label='成功率',tick_label = gprs_name_list,fc = 'lightskyblue')
for i in range(len(x)):    
    x[i] = x[i] + width
b2 = plt.bar(x, gprs_num_list1, width=width, label=u'占比',fc = 'yellowgreen')
plt.legend()
plt.xlabel('百分比')
plt.ylabel('船只個數(shù)')
for rect in b1+b2:
    h = rect.get_height()
    plt.text(rect.get_x()+rect.get_width()/2,h,'%d'%int (h),ha ='center',va='bottom')
# change to work path
os.chdir(path)
if dest_path is None:
    dest_path = os.path.join(path, '%s_%s' % (from_date.date(), to_date.date()))
if not os.path.exists(path):
    os.mkdir(path)
if not os.path.exists(dest_path):
    os.mkdir(dest_path)
outfn1 = os.path.join(dest_path, u"%s_%s漁船GPRS信號統(tǒng)計.png" % (from_date.date(), to_date.date()))
plt.savefig(outfn1,dpi=300)

plt.show()

plt.close(0)

lbs_success_count_100=0
lbs_success_count_90=0
lbs_success_count_80=0
lbs_success_count_70=0
lbs_success_count_60=0
lbs_success_count_50=0
lbs_success_count_40=0
lbs_success_count_30=0
lbs_success_count_20=0
lbs_success_count_10=0 
lbs_success_count_0=0 
lbs_success_100_lists_by_device_no=[]
lbs_success_90_lists_by_device_no=[]
lbs_success_80_lists_by_device_no=[]
lbs_success_70_lists_by_device_no=[]
lbs_success_60_lists_by_device_no=[]
lbs_success_50_lists_by_device_no=[]
lbs_success_40_lists_by_device_no=[]
lbs_success_30_lists_by_device_no= []
lbs_success_20_lists_by_device_no= []
lbs_success_10_lists_by_device_no=[]
lbs_success_0_lists_by_device_no= []
lbs_success = 0.0
lbs_time_count_100=0
lbs_time_count_90=0
lbs_time_count_80=0
lbs_time_count_70=0
lbs_time_count_60=0
lbs_time_count_50=0
lbs_time_count_40=0
lbs_time_count_30=0
lbs_time_count_20=0
lbs_time_count_10=0 
lbs_time_count_0=0 
lbs_time_percent = 0.0

for j in range(1,nrows-1):
    if not sheet.cell(j,5).value == '':
        lbs_success = sheet.cell(j,5).value
    else:
        lbs_success = ''
    if not lbs_success == '':
        if(lbs_success == 1.0):
            lbs_success_count_100+=1
            lbs_success_100_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success < 1.0 and lbs_success >= 0.9):
            lbs_success_count_90+=1
            lbs_success_90_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success < 0.9 and lbs_success>=0.8):
            lbs_success_count_80+=1
            lbs_success_80_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success< 0.8 and lbs_success>=0.7):
            lbs_success_count_70+=1
            lbs_success_70_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.7 and lbs_success>=0.6):
            lbs_success_count_60+=1
            lbs_success_60_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success< 0.6 and lbs_success>=0.5):
            lbs_success_count_50+=1
            lbs_success_50_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.5 and lbs_success>= 0.4):
            lbs_success_count_40+=1
            lbs_success_40_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.4 and lbs_success>=0.3):
            lbs_success_count_30+=1
            lbs_success_30_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.3 and lbs_success>=0.2):
            lbs_success_count_20+=1
            lbs_success_20_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.2 and lbs_success>=0.1):
            lbs_success_count_10+=1
            lbs_success_10_lists_by_device_no.append(sheet.cell(j,0).value)
        if(lbs_success<0.1):
            lbs_success_count_0+=1
            lbs_success_0_lists_by_device_no.append(sheet.cell(j,0).value)
    if not sheet.cell(j,6).value == '':
        lbs_time_percent = sheet.cell(j,6).value
    else:
        lbs_time_percent = ''
    if not lbs_time_percent == '':
        if(lbs_time_percent == 1.0):
            lbs_time_count_100+=1
        if(lbs_time_percent < 1.0 and lbs_time_percent >= 0.9):
            lbs_time_count_90+=1
        if(lbs_time_percent < 0.9 and lbs_time_percent>=0.8):
            lbs_time_count_80+=1
        if(lbs_time_percent< 0.8 and lbs_time_percent>=0.7):
            lbs_time_count_70+=1
        if(lbs_time_percent<0.7 and lbs_time_percent>=0.6):
            lbs_time_count_60+=1
        if(lbs_time_percent< 0.6 and lbs_time_percent>=0.5):
            lbs_time_count_50+=1
        if(lbs_time_percent<0.5 and lbs_time_percent>= 0.4):
            lbs_time_count_40+=1
        if(lbs_time_percent<0.4 and lbs_time_percent>=0.3):
            lbs_time_count_30+=1
        if(lbs_time_percent<0.3 and lbs_time_percent>=0.2):
            lbs_time_count_20+=1
        if(lbs_time_percent<0.2 and lbs_time_percent>=0.1):
            lbs_time_count_10+=1
        if(lbs_time_percent<0.1):
            lbs_time_count_0+=1
lbs_name_list = ['100%','90%','80%','70%','60%','50%','40%','30%','20%','10%','<10%']
lbs_num_list = [lbs_success_count_100,lbs_success_count_90,lbs_success_count_80,lbs_success_count_70,lbs_success_count_60,lbs_success_count_50,lbs_success_count_40,lbs_success_count_30,lbs_success_count_20,lbs_success_count_10,lbs_success_count_0]
lbs_num_list1 = [lbs_time_count_100,lbs_time_count_90,lbs_time_count_80,lbs_time_count_70,lbs_time_count_60,lbs_time_count_50,lbs_time_count_40,lbs_time_count_30,lbs_time_count_20,lbs_time_count_10,lbs_time_count_0]
x =list(range(len(lbs_num_list)))
total_width, n = 0.8, 2
width = total_width / n 
#指定輸出圖片的尺寸
plt.figure(figsize=(14, 7))
# 設置標題
plt.title(u"漁船北斗信號統(tǒng)計   設備類型:%s 時間段:%s ~ %s" % (dev_type,from_date.date(), to_date.date()), **kwargs)
b1 = plt.bar(x, lbs_num_list, width=width,label='成功率',tick_label = lbs_name_list,fc = 'lightskyblue')
for i in range(len(x)):    
    x[i] = x[i] + width
b2 = plt.bar(x, lbs_num_list1, width=width, label=u'占比',fc = 'yellowgreen')
plt.legend()
plt.xlabel('百分比')
plt.ylabel('船只個數(shù)')
for rect in b1+b2:
    h = rect.get_height()
    plt.text(rect.get_x()+rect.get_width()/2,h,'%d'%int (h),ha ='center',va='bottom')
# change to work path
outfn2 = os.path.join(dest_path, u"%s_%s漁船北斗信號統(tǒng)計.png" % (from_date.date(), to_date.date()))
plt.savefig(outfn2,dpi=300)

plt.show()

plt.close(0)

libv_average_count_6=0
libv_average_count_5=0
libv_average_count_4=0
libv_average_count_3=0
libv_average_count_2=0
libv_average_count_1=0
libv_average_count_0=0

libv_average_6_lists_by_device_no=[]
libv_average_5_lists_by_device_no=[]
libv_average_4_lists_by_device_no=[]
libv_average_3_lists_by_device_no=[]
libv_average_2_lists_by_device_no=[]
libv_average_1_lists_by_device_no=[]
libv_average_0_lists_by_device_no=[]
libv_average = 0.0

pvbv_average_count_3=0
pvbv_average_count_2_5=0
pvbv_average_count_2=0
pvbv_average_count_1_5=0
pvbv_average_count_1=0
pvbv_average_count_0_5=0
pvbv_average_count_0=0
pvbv_average_3_lists_by_device_no=[]
pvbv_average_2_5_lists_by_device_no=[]
pvbv_average_2_lists_by_device_no=[]
pvbv_average_1_5_lists_by_device_no=[]
pvbv_average_1_lists_by_device_no=[]
pvbv_average_0_5_lists_by_device_no=[]
pvbv_average_0_lists_by_device_no=[]
pvbv_average = 0.0

for j in range(1,nrows-1):
    if not sheet.cell(j,7).value == '':
        libv_average = sheet.cell(j,7).value
    else:
        libv_average = ''
    if not libv_average == '':
        if(libv_average>= 4.1):
            libv_average_count_6+= 1
            libv_average_6_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average < 4.1 and libv_average >=4.0):
            libv_average_count_5+= 1
            libv_average_5_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average < 4.0 and libv_average >=3.9):
            libv_average_count_4+= 1
            libv_average_4_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average <3.9 and libv_average >=3.8):
            libv_average_count_3+= 1
            libv_average_3_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average < 3.8 and libv_average >=3.7):
            libv_average_count_2 += 1
            libv_average_2_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average < 3.7 and libv_average >= 3.6):
            libv_average_count_1 += 1
            libv_average_1_lists_by_device_no.append(sheet.cell(j,0).value)
        if(libv_average < 3.6):
            libv_average_count_0 += 1
            libv_average_0_lists_by_device_no.append(sheet.cell(j,0).value)

    if not sheet.cell(j,8).value == '':
        pvbv_average = sheet.cell(j,8).value
    else:
        pvbv_average = ''
    if not pvbv_average == '':
        if(pvbv_average < 2.75 and pvbv_average >= 2.25):
            pvbv_average_count_3+= 1
            pvbv_average_3_lists_by_device_no.append(sheet.cell(j,1).value)
        if(pvbv_average < 2.25 and pvbv_average >= 1.75):
            pvbv_average_count_2_5+= 1
            pvbv_average_2_5_lists_by_device_no.append(sheet.cell(j,1).value)
        if(pvbv_average < 1.75 and pvbv_average >= 1.25):
            pvbv_average_count_2+= 1
            pvbv_average_2_lists_by_device_no.append(sheet.cell(j,1).value)
        if(pvbv_average < 1.25 and pvbv_average >= 0.75):
            pvbv_average_count_1_5+= 1
            pvbv_average_1_5_lists_by_device_no.append(sheet.cell(j,1).value)
        if(pvbv_average < 0.75 and pvbv_average >= 0.65):
            pvbv_average_count_1 += 1
            pvbv_average_1_lists_by_device_no.append(sheet.cell(j,1).value)
        if(pvbv_average < 0.65 and pvbv_average >= 0.45):
            pvbv_average_count_0_5 += 1
            pvbv_average_0_5_lists_by_device_no.append(sheet.cell(j,0).value)
        if(pvbv_average < 0.45 and pvbv_average >= 0.0):
            pvbv_average_count_0 += 1
            pvbv_average_0_lists_by_device_no.append(sheet.cell(j,1).value)
print('pvbv_average_count_0_5------------'+str(pvbv_average_count_0_5))
print(pvbv_average_0_5_lists_by_device_no)
lbs_name_list = ['大于4.1(2.5)','4.0-4.1(2.0)','3.9-4.0(1.5)','3.8-3.9(1.0)','3.7-3.8(0.7)','3.6-3.7(0.5)','小于3.6(小于0.45)']
lbs_num_list = [libv_average_count_6,libv_average_count_5,libv_average_count_4,libv_average_count_3,libv_average_count_2,libv_average_count_1,libv_average_count_0]
lbs_num_list1 = [pvbv_average_count_3,pvbv_average_count_2_5,pvbv_average_count_2,pvbv_average_count_1_5,pvbv_average_count_1,pvbv_average_count_0_5,pvbv_average_count_0]
x =list(range(len(lbs_num_list)))
total_width, n = 0.8, 2
width = total_width / n 
#指定輸出圖片的尺寸
plt.figure(figsize=(14, 7))
# 設置標題
plt.title(u"電池電壓統(tǒng)計   設備類型:%s 時間段:%s ~ %s" % (dev_type,from_date.date(), to_date.date()), **kwargs)
b1 = plt.bar(x, lbs_num_list, width=width,label='鋰電池電壓',tick_label = lbs_name_list,fc = 'lightskyblue')
for i in range(len(x)):    
    x[i] = x[i] + width
b2 = plt.bar(x, lbs_num_list1, width=width, label=u'光伏電池電壓',fc = 'yellowgreen')
plt.legend()
plt.xlabel('電壓值')
plt.ylabel('船只數(shù)')
for rect in b1+b2:
    h = rect.get_height()
    plt.text(rect.get_x()+rect.get_width()/2,h,'%d'%int (h),ha ='center',va='bottom')
# change to work path
outfn2 = os.path.join(dest_path, u"%s_%s漁船電池電壓統(tǒng)計.png" % (from_date.date(), to_date.date()))
plt.savefig(outfn2,dpi=300)

plt.show()

plt.close(0)

def get_parser():
from optparse import OptionParser
parser = OptionParser("Usage: %prog [options] from_date to_date ")
parser.add_option("-l", "--use-local", dest="use_local", default=False,
action="store_true", help="whether to use local data for processing")
parser.add_option("-p", "--working-path", dest="path", default='.',
help="working path to save or load data, default: ./")
parser.add_option("-i", "--device-no", dest="device_nos", default=[], action='append',
help="ship identities, seperated by comma, string like 211,222,232")
parser.add_option("-r", "--replace", dest="replace", default=False,
action="store_true", help="to replace any result already done")
return parser

if name == 'main':

sys.argv = ['', '2018-10-26', '2018-10-28'] 
parser = get_parser()
(options, argv) = parser.parse_args(sys.argv)

from_date = parse_datetime(argv[1])
to_date = parse_datetime(argv[2])   

dev_type = 3

device_nos = get_device_nos(dev_type)

threads=[]

num = 1

for i in list(chunks(device_nos, int(len(device_nos)/12))):

threads.append(threading.Thread(target=get_excel,args=(i,num) ))

num += 1

for thread in threads:

thread.start()

for thread in threads:

thread.join()

excels_toOne_excel()

draw_signal(dev_type, from_date, to_date)

num = 1

future = []

pool = ThreadPoolExecutor(max_workers=12)

for i in list(chunks(device_nos, int(len(device_nos)/12))):

future.append(pool.submit(get_excel,i,num))

num += 1

for f in future:

if f.running():

print('線程%s is running' % str(f))

for f in as_completed(future):

try:

ret = f.done()

if ret:

f_ret = f.result()

print('%s, done, result: %s, %s' % (str(f), f_ret.num, len(f_ret.content)))

except Exception as e:

f.cancel()

print(str(e))

excels_toOne_excel()

draw_signal(dev_type, from_date, to_date)

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末步脓,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子浩螺,更是在濱河造成了極大的恐慌靴患,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件要出,死亡現(xiàn)場離奇詭異鸳君,居然都是意外死亡,警方通過查閱死者的電腦和手機患蹂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門或颊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人传于,你說我怎么就攤上這事囱挑。” “怎么了沼溜?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵平挑,是天一觀的道長。 經(jīng)常有香客問我系草,道長弹惦,這世上最難降的妖魔是什么否淤? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮棠隐,結果婚禮上,老公的妹妹穿的比我還像新娘檐嚣。我一直安慰自己助泽,他們只是感情好,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布嚎京。 她就那樣靜靜地躺著嗡贺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鞍帝。 梳的紋絲不亂的頭發(fā)上诫睬,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機與錄音帕涌,去河邊找鬼摄凡。 笑死,一個胖子當著我的面吹牛蚓曼,可吹牛的內(nèi)容都是我干的亲澡。 我是一名探鬼主播,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼纫版,長吁一口氣:“原來是場噩夢啊……” “哼床绪!你這毒婦竟也來了?” 一聲冷哼從身側響起其弊,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤癞己,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后梭伐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體痹雅,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年籽御,在試婚紗的時候發(fā)現(xiàn)自己被綠了练慕。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡技掏,死狀恐怖铃将,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情哑梳,我是刑警寧澤劲阎,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站鸠真,受9級特大地震影響悯仙,放射性物質(zhì)發(fā)生泄漏龄毡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一锡垄、第九天 我趴在偏房一處隱蔽的房頂上張望沦零。 院中可真熱鬧,春花似錦货岭、人聲如沸路操。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽屯仗。三九已至,卻和暖如春搔谴,著一層夾襖步出監(jiān)牢的瞬間魁袜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工敦第, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峰弹,地道東北人。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓申尼,卻偏偏與公主長得像垮卓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子师幕,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容