为处理千万数量级数据,编制了相应代码。
#################################################################
#
# 为了处理ESG大量数据,选择采用Python作为处理工具
# # coded by:zhuquanjie
# datetime:20140705
# @guiyang
#
##################################################################
import numpy as np
import matplotlib.pyplot as plt
import os
#from matplotlib.matlab import *
# 打开读取的文件夹
f = open("G://GoodCode//test2.txt"); # 读文件
lines = f.readlines();
Llen = len(lines);
# 设定好存储的字典
a = []; # channel num
b = []; # ??
c = []; # data time
d = []; # amplitude
# 设定从第几行读取,直到第几行停止
interval = 4; # 采样间隔,每一毫秒采样4次
#l_list = line[0:]; # 从第几行开始读取
startTime = 201944; #横轴为时间,因此,需要计算点数
startPoint = int(startTime*interval);
endTime = 205560; #横轴为时间,因此,需要计算点数
endPoint = int(endTime*interval);
# 连续读取数据(以行为单位)
ind = 1;
for i in range(startPoint, endPoint): # 设定波形所取位置
#if ind<=100
# if l.find('')
print 'line: ', ind
ind += 1;
# lines = f.readlines();
line = lines[i];
line1 = line.strip('\n'); # 去掉数据后的换行符
m = line1.split(' ');
#m = map(float,n)
temD = []; # 临时字典,存放数据
for ii in range(0, len(m)):
if m[ii] != '':
temD.append(m[ii]);
print 'temD:
', temD
'\n'
#return
True
#if len(temD) == 4:
a.append(int(temD[0])); # 通道号
b.append(int(temD[1])); # 未知??
c.append(float(temD[2])); # 时间
d.append(float(temD[3])); # 振幅值
# d.append(float(m[9]))
# print 'this is the results: ',a,b,c,d
f.close();
print 'this is the results: ',a,b,c,d
# 数据存储总文件夹
os.mkdir('files2');
# 按通道、数据类别分别存储数据
for i in range(1, 12):
Eamplitude = [];
Etime = [];
for j in range(1, len(b)):
#wid = a[j]; # 通道号
if int(a[j]) == i:
Etime.append(float(c[j]));
Eamplitude.append(float(d[j]));
wid =
a[j];
else:
wid =
i;
# 存储时间
savename =
'files2\\time'+str(wid)+'.txt'; wr = open(savename, 'w');
wr.write(str(Etime));
wr.close();
# 存储振幅
savename =
'files2\\amplitude'+str(wid)+'.txt'; wr = open(savename, 'w');
wr.write(str(Eamplitude));
wr.close();
# 新建欲存储数据的文件夹
wr = open('ddd.txt', 'w');
wr.write(str(d));
wr.close();
# 绘图
plt.figure(1) # 创建图表1
plt.rcParams["font.family"] = "Times New Roman"
plt.plot(c,d, '-', label = 'Channel'+str(b[1]));
plt.legend(); # display legend
plt.grid(True);
plt.title('ESG Microseismic Waveform');
plt.xlabel('Time', fontsize=18, color='black' ,
multialignment='center');
plt.ylabel('Amplitude', fontsize=18, color='black' ,
multialignment='center');
#plt.suptitle('Channel'+str(b), y=0.97); # 添加标题
plt.text(2.5, 0.00003, 'Channel'+str(b[1])); # 添加线条注释
figname = 'ESG波形图.png'
plt.show();
plt.savefig(figname, dpi=96);