说明
1. 主要是将TDC激光测距数据进行统计叠加并绘制为图片,便于直观的分析与观察
一、定义全局变量变
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 = [ ]
OrgDatStop2Mem = [ ]
DIST1_FIX_SUPER_MAX_VAL_M = 3000
DIST1_RANGE_SUPER_MAX_VAL_M = 3000
DIST2_FIX_SUPER_MAX_VAL_M = 3000
DIST2_RANGE_SUPER_MAX_VAL_M = 3000
DIST1_DIST2_RANGE_SUPER_MAX_VAL_M = 3000
DIST2_DIST1_DIFF_SUPER_MAX_VAL_DM = 3000
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)
nameOnly = os. path. splitext( nameExten) [ 0 ]
newDir = os. path. join( RES_DAT_STORE_DIR, nameOnly)
if os. path. exists( newDir) == False :
os. mkdir( newDir)
else :
Delete_Directory_Content( newDir)
newInfoFile = newDir + os. path. sep + nameOnly + RES_INFO_FILE_EXTENSION
with open ( newInfoFile, 'w' ) :
pass
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
def AppendData_Stop1Stop2 ( stop1, stop2) :
global OrgDatStop1Mem
global OrgDatStop2Mem
OrgDatStop1Mem. append( stop1)
OrgDatStop2Mem. append( stop2)
def ClearData_Stop1Stop2 ( ) :
global OrgDatStop1Mem
global OrgDatStop2Mem
OrgDatStop1Mem. clear( )
OrgDatStop2Mem. clear( )
def OrgData_FileHandler ( orgDatFile, resInfoDir, resInfoFile) :
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