用Python处理TDC激光测距数据并绘制为图片

Python处理TDC激光测距数据并绘图

说明

1. 主要是将TDC激光测距数据进行统计叠加并绘制为图片,便于直观的分析与观察

一、定义全局变量变

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import datetime
import numpy as np
import os
import datetime
import shutil


ORG_DAT_STORE_DIR = 'DataHandlerDir'    #原始文件目录
RES_DAT_STORE_DIR = 'ImageCreatDir'     #处理文件目录
RES_INFO_FILE_EXTENSION  = '.log'       #原始文件后缀名
RES_INFO_IMAGE_EXTENSION = '.png'       #处理文件后缀名


SUPER_CALC_DIST_GAP_POINT = 2
SUPER_PEAK_GAP_POINT = 4
DIST_SUPER_GAP_M = 2 #距离叠加的间隔

OrgDatStop1Mem = [] #Stop1原始数据缓存
OrgDatStop2Mem = [] #Stop2原始数据缓存


DIST1_FIX_SUPER_MAX_VAL_M   = 3000 #距离1(Stop1)固定叠加的最大值
DIST1_RANGE_SUPER_MAX_VAL_M = 3000 #距离1(Stop1)范围叠加的最大值

DIST2_FIX_SUPER_MAX_VAL_M   = 3000 #距离2(Stop2)固定叠加的最大值
DIST2_RANGE_SUPER_MAX_VAL_M = 3000 #距离2(Stop2)范围叠加的最大值

DIST1_DIST2_RANGE_SUPER_MAX_VAL_M = 3000 #距离1~距离2(Stop1~Stop2)范围叠加的最大值
DIST2_DIST1_DIFF_SUPER_MAX_VAL_DM = 3000 #距离2-距离1(Stop2-Stop1)脉宽叠加的最大值

CreatImageCount = 0
OrgStaInfo = ""

#原始文件
OrgDataFileNameGroup = \
[
    r"DataHandlerDir/xPythonDemoTest.log",
]

二、主函数入口


#删除目录内容
def Delete_Directory_Content(dir):
    if os.path.exists(dir) == True: #目录存在
        for item in os.listdir(dir): #目录中内容
            name = os.path.join(dir, item) #拼接完整路径
            if os.path.isfile(name):
                os.remove(name) #删除目录
            elif os.path.isdir(name):
                shutil.rmtree(name) #删除文件



# 原始数据处理
def Original_Data_Handler(nameGroup):
    global RES_DAT_STORE_DIR
    global RES_INFO_FILE_EXTENSION

    if len(nameGroup) <= 0:
        print('No Need Handler Files......')
        return

    for name in nameGroup:
        nameExten = os.path.basename(name) #结果文件名+后缀名 xTest.log
        nameOnly  = os.path.splitext(nameExten)[0] #结果文件名 xTest
        newDir    = os.path.join(RES_DAT_STORE_DIR, nameOnly) #结果文件路径 #ImageCreatDir\xTest

        # 创建目录
        if os.path.exists(newDir) == False:
            os.mkdir(newDir)
        else:
            Delete_Directory_Content(newDir)

        # 创建文件 结果文件 ImageCreatDir\xTest\xTest.log
        newInfoFile = newDir + os.path.sep + nameOnly + RES_INFO_FILE_EXTENSION
        with open(newInfoFile, 'w'):
            pass

        # print("==1==>  " + name)        #DataHandlerDir/xTest.log
        # print("==2==>  " + newDir)      #ImageCreatDir\xTest
        # print("==3==>  " + newInfoFile) #ImageCreatDir\xTest\xTest.log
        # print("==4==>  " + nameOnly)    #xTest
        # print("==5==>  " + nameExten)   #xTest.log

        print(r'Start Handler  ====>  ', name)
        OrgData_FileHandler(name, newDir, newInfoFile)



def main():
    global ORG_DAT_STORE_DIR
    global RES_DAT_STORE_DIR
    global OrgDataFileNameGroup

    if os.path.exists(ORG_DAT_STORE_DIR) == False:
        os.mkdir(ORG_DAT_STORE_DIR)
        return
    
    if os.path.exists(RES_DAT_STORE_DIR) == False:
        os.mkdir(RES_DAT_STORE_DIR)
        return

    Original_Data_Handler(OrgDataFileNameGroup)
    print('All Original Data Files Handler Complete......')




if __name__ == '__main__':
    main()

三、处理原始文件数据


#是否为数字
def Judge_IsDigit(orgData):
    for dat in orgData:
        if dat.isdigit() == False:
            return 1
    return 0


#追加数据到Stop1-Stop2缓存
def AppendData_Stop1Stop2(stop1, stop2):
    global OrgDatStop1Mem
    global OrgDatStop2Mem

    OrgDatStop1Mem.append(stop1)
    OrgDatStop2Mem.append(stop2)

#清空Stop1-Stop2缓存
def ClearData_Stop1Stop2():
    global OrgDatStop1Mem
    global OrgDatStop2Mem

    OrgDatStop1Mem.clear()
    OrgDatStop2Mem.clear()



#原始文件处理
def OrgData_FileHandler(orgDatFile,resInfoDir,resInfoFile):
    # print("orgDatFile : " + orgDatFile)  #DataHandlerDir/xTest.log
    # print("resInfoDir : " + resInfoDir)  #ImageCreatDir\xTest
    # print("resInfoFile: " + resInfoFile) #ImageCreatDir\xTest\xTest.log

    global OrgDatStop1Mem
    global OrgDatStop2Mem
    global CreatImageCount
    global OrgStaInfo

    if os.path.exists(orgDatFile) == False: #文件不存在
        return
   
    CreatImageCount = 0
    orgDataCount    = 0
    ClearData_Stop1Stop2()
    with open(orgDatFile,'r', encoding='utf-8') as fileHander:#读方式打开文件
        for lineTxt in fileHander: #行方式读取文件内容
            if len(lineTxt.strip()) <= 30: #一行数据太少
                continue

            if '[' not in lineTxt: #不存在[
                continue

            if ']' not in lineTxt: #不存在]
                continue

            separDat = lineTxt.replace(']', ', ').replace('[', '') #将[]替换
            orgData  = [spData.strip() for spData in separDat.split(",")] #以,拆分
            if Judge_IsDigit(orgData) == 0:#全为数字
                if int(orgData[0]) == 0:
                    if len(OrgDatStop1Mem) > 10:
                        OrgStaInfo = lineTxt
                        OrgData_CreateImage(OrgDatStop1Mem, OrgDatStop2Mem, resInfoDir,resInfoFile)

                    OrgStaInfo = ""
                    orgDataCount = 0
                    ClearData_Stop1Stop2()
                    AppendData_Stop1Stop2(orgData[1], orgData[2])
                else:
                    orgDataCount = orgDataCount + 1
                    if orgDataCount == int(orgData[0]):
                        AppendData_Stop1Stop2(orgData[1], orgData[2])
                    else:
                        OrgStaInfo   = ""
                        orgDataCount = 0
                        ClearData_Stop1Stop2()
            else:
                if len(OrgDatStop1Mem) > 10:
                    OrgStaInfo = lineTxt
                    OrgData_CreateImage(OrgDatStop1Mem, OrgDatStop2Mem, resInfoDir,resInfoFile)
                
                OrgStaInfo = ""
                orgDataCount = 0
                ClearData_Stop1Stop2()

四、将数据叠加统计生成图片

# 生成图片
def OrgData_CreateImage(stop1, stop2, resInfoDir,resInfoFile):
    global RES_INFO_IMAGE_EXTENSION
    global CreatImageCount
    global OrgStaInfo

    stop1Org = np.array([int(i) for i in stop1])
    stop1Dat = SuperAnalyse_Stop1FixPoint(stop1Org
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值