@zhouhuibin
2023-03-15T15:11:53.000000Z
字数 2537
阅读 208
博士后出站报告附录
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 21 10:50:15 2021
@author: zhouhuibin
"""
import matplotlib.pyplot as plt
import numpy as np
import math
import os
from zhbcode import selffunction as sf
from zhbcode.find_data import load_weidata
from tkinter import _flatten # 将二维列表转换为一维列表
maindir = r'D:\CSNS\Summary\Data Treatment\python\Moderator'
datapath = sf.mkdir(maindir+r'\data')
figpath = sf.mkdir(maindir+r'\figs')
datafile = r'D:\CSNS\Summary\Data Treatment\python\Moderator\MCSTAS-DPHM-BL10.dat'
ifile = open (datafile,'r') # 以只读格式打开文件“datafile”
lines= ifile.readlines() # 按行读取文件,形成一个list,每一行是list的一个元素
line_no = 0
found_string = False
for line in lines:
if '[Data]' in line: # 判断一个特定的行line中是否含有关键词key_word,只要含有就行,不一定要完全等于
#if 'Tableau' in line:
print ('found in line '+ str(line_no))
found_string = True
break
line_no += 1
if not found_string:
print ('string not found');#sys.exit(1)
numbers = []
row_mins = [line_no+1]
row_maxs = []
num_wave = 0
j = 0
data = []
num_line = line_no
name = []
unit = []
comment = []
fwhms = []
wavelengths = []
energys = []
t_centers = []
for line in lines[line_no+1:]: # line_no为发现key_word的行,从line_no+1行,即真正数据开始行开始,每一行分别进行如下操作
num_line = num_line+1
if len(line) == 5:
num_wave = num_wave+1
row_empty = num_line
row_maxs.append(row_empty - 1)
row_mins.append(row_empty + 1)
row_maxs.append(len(lines)-1)
for i in range(len(row_mins)):
numbers = []
for line in lines[row_mins[i]:row_maxs[i]+1]:
words = line.split()
numbers_row = []
for i1 in range(len(words)):
number = float(words[i1])
numbers_row.append(number) # 创建每一行的数据数组
numbers.append(numbers_row) # 结合每一行的数据形成二维数据数组
numbers = np.array(numbers)
Time = numbers[:,0]
Time = np.delete(Time, 0)
Brightness = numbers[:,2]
Brightness = np.delete(Brightness, 0)
spec_para = sf.FWHM(Time,Brightness)
fwhm = spec_para[0]
t_center = spec_para[1]
t_centers.append(t_center)
# if i == 13:
# print (fwhm,t_center)
fwhms.append(fwhm)
numbers = np.transpose(numbers)
numbers = list(numbers)
data = data + numbers
energy = numbers[1][0]
energys.append(energy)
wavelength = np.sqrt(81.81/(1e3*energy))
wavelengths.append(wavelength)
name = name+['Time','Energy','Brightness','Std.error',]
unit = unit+['us','eV','n/p/us/eV','n/p/us/eV',]
comment = comment+[(f'{i}-{energy}eV-{wavelength}AA',)*4]
ifile.close()
name = _flatten(name)
unit = _flatten(unit)
comment = _flatten(comment)
data = np.transpose(data)
filename = 'Beam10_Moderator_Spectrums.dat'
sf.dataoutput(name,unit,comment,data,datapath,filename)
name_fwhm = ['wavelength','energy','t_center','FWHM']
unit_fwhm = ['AA','eV','us','us']
comment_fwhm = ['DPHM-BL10','DPHM-BL10','DPHM-BL10','DPHM-BL10']
data_fwhm = np.c_[wavelengths,energys,t_centers,fwhms]
filename = 'Beam10_DPHM_FWHM.dat'
sf.dataoutput(name_fwhm,unit_fwhm,comment_fwhm,data_fwhm,datapath,filename)