Skip to content

Instantly share code, notes, and snippets.

@renexdev
Created September 30, 2016 20:09
Show Gist options
  • Save renexdev/e04e31dad78b97337e93100472c7be76 to your computer and use it in GitHub Desktop.
Save renexdev/e04e31dad78b97337e93100472c7be76 to your computer and use it in GitHub Desktop.
Calibration curve fitting and interpolating magnetization values
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
import sys
sys.path.append("./modules/")
####################################################################################################################################
filename = '08.0K_20x_01_d1_intensity_v_01'
#08.0K_20x_01_d1_intensity_h_02
#08.0K_20x_01_d1_intensity_v_01
#08.0K_20x_01_d1_intensity_v_02
filenameOut = '08.0K_20x_01_d1_gauss_v_01'
dataIn = np.loadtxt(filename+".dat")
####################################################################################################################################
writeIt = open(filenameOut+".dat", "w")
relPath = "./"
filename = 'd01_Hyst_08.0K_20x_01_IvsH_bkgId_2_01'
#filename = 'd01_Hyst_08.0K_20x_01_IvsH_bkgId_2_02'
calCurve = np.loadtxt(relPath+filename+".dat")
Int = np.array([calCurve[i,0] for i in range(len(calCurve[:,0]))])
Ha = np.array([calCurve[i,1] for i in range(len(calCurve[:,0]))])
correction = min(Int)-34520.0
Int=Int-correction
#34520.0
tck = interpolate.splrep(Int, Ha, s=0)
yPy = interpolate.splev(Int, tck, der=0)
lenRow = dataIn.shape[0]
lenCol = dataIn.shape[1]
minVals = []
spline3MinVals = []
maxVals = []
spline3MaxVals = []
print Int[1]-Int[0]
for j in range(lenCol):
writeIt.write("%.2f\t" % (dataIn[0,j])) #800
writeIt.write("\n")
for i in range(1,lenRow):
writeIt.write("%.2f\t" % (dataIn[i,0])) #800
for j in range(1,lenCol):
if(dataIn[i,j]<min(Int)):
minVals.append(dataIn[i,j])
spline3MinVals.append(interpolate.splev(dataIn[i,j], tck, der=0))
print "MIN %d %d (%.2f G) - value %d- min %d - dif %d"%(i,j,dataIn[0,j],dataIn[i,j],min(Int),dataIn[i,j]-min(Int))
if(dataIn[i,j]>max(Int)):
maxVals.append(dataIn[i,j])
spline3MaxVals.append(interpolate.splev(dataIn[i,j], tck, der=0))
print "MAX %d %d (%.2f G) - value %d- max %d - dif %d"%(i,j,dataIn[0,j],dataIn[i,j],max(Int),dataIn[i,j]-max(Int))
writeIt.write("%.4f\t" % (interpolate.splev(dataIn[i,j], tck, der=0))) #800
#writeIt.write("%.4f\t" % (interpolate.splev(dataIn[i,j], tck, der=0))) #800
writeIt.write("\n")
writeIt.close()
if(len(minVals)!=0): print min(minVals)
plt.plot(Int, Ha, 'o',Int, yPy , 'b',minVals,spline3MinVals ,'ko' ,maxVals,spline3MaxVals ,'go')
plt.legend(['data', 'cubic spline python'], loc='best')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment