Commit d7bf0976 authored by Zhang, Yuanpeng's avatar Zhang, Yuanpeng
Browse files

new claib

parent 5de2cd43
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
version=1
freq      wl        van       van_back    mt_env    mt_instr  PAC03     PAC06        PAC08      PAC10     N/A       
60        0.8       0          0         0         0         0         0         0         0         0         
60        1.5       0          0           0         0         0         0             0         0         0         
60        2.665     61794    61758         0         0         0         61795         0         0         0         
60        4.797     0          0           0         0         0         0         0         0         0         
+6 −0
Original line number Diff line number Diff line
version=1
freq      wl        van       van_back    mt_env    mt_instr  PAC03     PAC06        PAC08      PAC10     N/A       
60        0.8       61626    61619         0         0         0         61623         0         0         0         
60        1.5       0          0           0         0         0         0             0         0         0         
60        2.665     61794    61758         0         0         0         61622         0         0         0         
60        4.797     55483    55436         0         0         0         61624         0         0         0         
+1 −1
Original line number Diff line number Diff line
version=1
freq wl     van   van_back mt_env mt_instr PAC03 PAC06   PAC08   PAC10 PAC10withInsert N/A  VanadiumSampleCan 
60 0.800   60637  60634     0      0      0      60641  60644    60640    60666        0             0
60 1.500   60638  60635     0      0      0      60642  60645    0        60667        0             0
60 1.500   60638  60635     0      0      0      60642  60645    61868    60667        0             0
60 2.665   60639  60636     0      0      0      60643  60665    0        60668        0             0
60 4.797     0     0        0      0      0       0       0      0          0          0             0
+44 −0
Original line number Diff line number Diff line
"""
This script saves sliced diffraction data from POWGEN
and all sample temperature values sorted by slices for errobar of sample temperatures.
Author: Tsung-Han Yang @ ORNL
Date: March 2024
"""
# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
import csv
from mantid.api import AnalysisDataService as ADS

# Use this script in Mantidworkbench after you sliced your data
#################################################################################
##########      parameters for your scans
#################################################################################
scan_num = 45610 # scan number 
savepath = '/SNS/users/tt9/data/SNS/PG3/IPTS-24773/shared/example/' # save path
slice_num = 28 # number of slices
#################################################################################
#################################################################################

def read_log_values(ws,scan_num,ii,savepath,LogName='SampleTemp',save=True) :
    sample_temp_log = ws.getRun().getLogData(LogName)
    # Extract the time and value data
    time_data = sample_temp_log.times  # This gives you the time data
    temp_data = sample_temp_log.value  # This gives you the temperature data
    # If you want to save the data to a CSV file:
    if save==True :
        with open(savepath + 'PG3_{}_{:03d}_{}.csv'.format(scan_num,ii,LogName), 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(["Time (s)", LogName])
            writer.writerows(zip(time_data, temp_data))
        #file.close()

for ii in np.arange(slice_num) :
    ConvertUnits('PG3_{}_{}'.format(scan_num,ii),'dSpacing',OutputWorkspace='PG3_{}_{}'.format(scan_num,ii))
    #Minus('PG3_{}_0_split_{}'.format(scan_num,ii), 'bkg_scaled',OutputWorkspace='PG3_{}_0_split_{}'.format(scan_num,ii)) # background
    SaveAscii('PG3_{}_{}'.format(scan_num,ii), savepath+'PG3_{}_{:03d}.ascii'.format(scan_num,ii))
    temp_ws = ADS.retrieve('PG3_{}_{}'.format(scan_num,ii))
    read_log_values(temp_ws,scan_num,ii,savepath)
    
   
+160 −0
Original line number Diff line number Diff line
"""
Example: read and plot sliced data from POWGEN
Author: Tsung-Han Yang @ ORNL
Date: March 2024
"""
#/usr/bin/env python3
import matplotlib as plt
import numpy as np
import sys, glob
import csv
from scipy.optimize import curve_fit
#import matplotlib.font_manager

from matplotlib import rc
plt.rcParams['font.family'] = 'Dejavu Sans'
plt.rcParams['mathtext.fontset'] = 'dejavusans'
plt.rcParams['lines.linewidth']= 1
plt.rcParams['axes.facecolor'] = 'w'

# The path of your saved *.ascii and *.csv files
fpath = '/Users/tt9/Research/Mn3Ga/data/PG3_45610/10K_step/'
temp_unit = 'K' # 'C' if sample env. uses C as temperature unit 

fnames = glob.glob(fpath + '*.ascii')
fnames.sort()
fnames.reverse()

SampleTemps = glob.glob(fpath + '*.csv')
SampleTemps.sort()
SampleTemps.reverse()

#fit_data = False

def read_temp(fname) :
    try:
        # Open the CSV file
        with open(fname, mode='r') as file:
            csv_reader = csv.reader(file)
            
            # Create lists to store the columns
            column1 = []
            column2 = []
            
            # Read each row
            for row in csv_reader:
                if len(row) != 2:
                    raise ValueError("Each row in the CSV file must have exactly two columns.")
                # Append the values to respective lists
                column1.append(row[0])
                column2.append(row[1])
            
            # Display the content
            print("Column 1:", column1)
            print("Column 2:", column2)
            
            # Return the columns as lists for further use
            return np.mean(np.float64(column2[1:])), np.std(np.float64(column2[1:]))
    except Exception as e:
        print(f"An error occurred: {e}")
'''
def gauss(x,a,b,c,bk) :
	#c = 2.211
	#bk = 13
	#bk = 12.7532
	#b = 3*2.83960843e-03
	result = a*np.exp(-(x-c)**2/b**2) + bk
	return result

def gauss3(x,a1,b1,c1,a2,b2,c2,a3,b3,c3,bk) :
	#c = 2.211
	#bk = 13
	#bk = 12.7532
	#b = 3*2.83960843e-03
	g1 = a1*np.exp(-(x-c1)**2/b1**2)
	g2 = a2*np.exp(-(x-c2)**2/b2**2)
	g3 = a3*np.exp(-(x-c3)**2/b3**2)
	result = g1 + g2 + g3 + bk
	return result

def bkgrnd(x,bk) :
	result = bk
	return result
'''

def d2q(xdata) :
	xdata = np.array(xdata)
	xdata = 2*np.pi / xdata
	return xdata

T_list = []
T_list_err = []
axis = []
axis_Q = []
data = []
errs = []

for jj in np.arange(len(fnames)) :
	f = open(fnames[jj],'r')
	lines = f.readlines()
	x = []
	y = []
	E = []
	for ii in np.arange(2,len(lines)) :
		ln = lines[ii].split(',')
		t1 = np.float64(ln[0])
		t2 = np.float64(ln[1])
		e1 = np.float64(ln[2])
		x.append(t1)
		y.append(t2)
		E.append(e1)
	T, T_err = read_temp(SampleTemps[jj])
	T_list.append(T)
	T_list_err.append(T_err)
	axis.append(x)
	axis_Q.append(d2q(x))
	data.append(y)
	errs.append(E)

axis = np.array(axis)
axis_Q = np.array(axis_Q)
data = np.array(data)
errs = np.array(errs)
T_list = np.array(T_list)
T_list_err = np.array(T_list_err)

if temp_unit=='C' :
    T_list = T_list + 273


# colormap for neutron diffraction
figz = plt.figure(figsize=(3.375,3.375*9/16))
zx = figz.add_subplot(111)

pcolorplot = zx.pcolormesh(axis_Q,T_list,data,vmin=0,vmax=8,cmap='viridis')
#pcolorplot = zx.pcolormesh(axis_Q,T_list,data,vmin=0,vmax=280,cmap='viridis')
zx.set_xlim([d2q(1.8),d2q(1.2)])
zx.xaxis.set_major_locator(MultipleLocator(0.5))
zx.xaxis.set_minor_locator(MultipleLocator(0.25))
zx.yaxis.set_major_locator(MultipleLocator(50))
zx.yaxis.set_minor_locator(MultipleLocator(10))
#fig.subplots_adjust(left = 0.15,right = 0.99, bottom = 0.22, top = 0.99,wspace =0.02,hspace = 0.30) # APS
figz.subplots_adjust(left = 0.15,right = 0.99, bottom = 0.22, top = 0.98,wspace =0.02,hspace = 0.30) # ACS
#figz.subplots_adjust(left = 0.32,right = 0.97, bottom = 0.18, top = 0.98,wspace =0.02,hspace = 0.30) # SciAdv

### plot params
zx.tick_params(axis='both',which='major',labelsize=8)
zx.spines['left'].set_linewidth(0.5)
zx.spines['right'].set_linewidth(0.5)
zx.spines['bottom'].set_linewidth(0.5)
zx.spines['top'].set_linewidth(0.5)

zx.tick_params(which='both', labelsize=9,
		labelbottom=True, labeltop=False, labelleft=True, labelright=False,
		bottom=True, top=True, left=True, right=True, direction='in')
zx.set_xlabel(r'Q ($\mathrm{\AA^{-1}}$)',fontsize=10)
zx.set_ylabel(r'T (K)',fontsize=10)


show()