Commit be683e58 authored by Andreas Freise's avatar Andreas Freise
Browse files

moving from shelve to pickle

parent 42810831
......@@ -6,7 +6,8 @@ from __future__ import unicode_literals
from pykat import finesse
from pykat.commands import *
import copy
import shelve
#import shelve
import pickle
import sys
import scipy.optimize
......@@ -89,10 +90,12 @@ def main():
print(" current results saved in: {0}".format(tmpresultfile))
# first the current kat file
kat.saveScript(tmpkatfile)
# now the result variables:
tmpfile = shelve.open(tmpresultfile, flag="c")
tmpfile[str('result')]=result
tmpfile.close()
with open(tmpresultfile, 'wb') as handle:
pickle.dump(result, handle)
# now the result variables (former version usuing shelve):
#tmpfile = shelve.open(tmpresultfile, flag="c")
#tmpfile[str('result')]=result
#tmpfile.close()
#---------------------------------------------------------------------------
......
......@@ -9,7 +9,8 @@ import pylab as pl
import scipy
from scipy.optimize import fmin
import numpy as np
import shelve
import pickle
#import shelve
import copy
import sys
......@@ -51,9 +52,11 @@ def main():
# loading data saved by master.py
kat.loadKatFile('asc_base2.kat')
try:
tmpfile = shelve.open(tmpresultfile, flag="c")
result=tmpfile[str('result')]
tmpfile.close()
#tmpfile = shelve.open(tmpresultfile, flag="c")
#result=tmpfile[str('result')]
#tmpfile.close()
with open(tmpresultfile, 'rb') as handle:
result = pickle.load(handle)
except: raise Exception("Could not open temprary results file {0}".format(tmpresultfile))
# overwriting some variables
......@@ -125,9 +128,11 @@ def main():
# first the current kat file
kat.saveScript(tmpkatfile)
# now the result variables:
tmpfile = shelve.open(tmpresultfile)
tmpfile[str('result')]=result
tmpfile.close()
#tmpfile = shelve.open(tmpresultfile)
#tmpfile[str('result')]=result
#tmpfile.close()
with open(tmpresultfile, 'wb') as handle:
pickle.dump(result, handle)
#-----------------------------------------------------------------------------------
......@@ -186,7 +191,7 @@ def asc_phases(tmpkat):
kat.WFS1_I.phase1=x[0]
out = kat.run()
signal = out["WFS1_I"]
print('\r minimising: function value %g ' % signal, end=' ')
print('\r minimising: function value {0:<16g}'.format(float(signal)), end='')
sys.stdout.flush()
return -1*abs(signal)
......@@ -194,7 +199,7 @@ def asc_phases(tmpkat):
kat.WFS2_I.phase1=x[0]
out = kat.run()
signal = out["WFS2_I"]
print('\r minimising: function value %g ' % signal, end=' ')
print('\r minimising: function value {0:<16g}'.format(float(signal)), end='')
sys.stdout.flush()
return -1*abs(signal)
......
......@@ -6,7 +6,7 @@ from __future__ import unicode_literals
from pykat import finesse
from pykat.commands import *
import numpy as np
import shelve
import pickle
import copy
import sys
import shutil
......@@ -48,9 +48,11 @@ def main():
# loading data saved by master.py
kat.loadKatFile('asc_base3.kat')
try:
tmpfile = shelve.open(tmpresultfile)
result=tmpfile[str('result')]
tmpfile.close()
with open(tmpresultfile, 'rb') as handle:
result = pickle.load(handle)
#tmpfile = shelve.open(tmpresultfile)
#result=tmpfile[str('result')]
#tmpfile.close()
except: raise Exception("Could not open temprary results file {0}".format(tmpresultfile))
kat.PDrefl_q.enabled = False
......@@ -103,10 +105,13 @@ def asc_large(tmpkat, mir_name):
shutil.copyfile(tmpfilename, backupname)
print(" current results saved in: {0}".format(tmpfilename))
tmpfile = shelve.open(tmpfilename)
tmpfile[str('out')]=out
tmpfile[str('maxtems')]=done_maxtems
tmpfile.close()
with open(tmpfilename, 'wb') as handle:
pickle.dump({ "out": out, "maxtems": done_maxtems}, handle)
#tmpfile = shelve.open(tmpfilename)
#tmpfile[str('out')]=out
#tmpfile[str('maxtems')]=done_maxtems
#tmpfile.close()
if __name__ == '__main__':
......
......@@ -7,7 +7,7 @@ from pykat import finesse
from pykat.commands import *
import pylab as pl
import numpy as np
import shelve
import pickle
import sys
import matplotlib
......@@ -16,94 +16,97 @@ formatter.ENG_PREFIXES[-6] = 'u'
import matplotlib.backends.backend_pdf
def printPDF(self, filename):
pdfp = matplotlib.backends.backend_pdf.PdfPages(filename)
pdfp.savefig(self,dpi=300,bbox_inches='tight')
pdfp.close()
pdfp = matplotlib.backends.backend_pdf.PdfPages(filename)
pdfp.savefig(self,dpi=300,bbox_inches='tight')
pdfp.close()
def main():
print("""
--------------------------------------------------------------
Example file for using PyKat to automate Finesse simulations
Finesse: http://www.gwoptics.org/finesse
PyKat: http://www.gwoptics.org/pykat
Run this file to plot the data generated with master3.py.
Andreas Freise 16.01.2014
--------------------------------------------------------------
""")
# shall we clear the workspace?
# %reset -f
# maybe close all plot windows?
# close('all')
# making these global during testing and debugging
#global kat
#global out
#global result
print("--------------------------------------------------------")
print(" Plotting ASC signals for large misalignments")
asc_large('ITM')
asc_large('ETM')
print("""
--------------------------------------------------------------
Example file for using PyKat to automate Finesse simulations
Finesse: http://www.gwoptics.org/finesse
PyKat: http://www.gwoptics.org/pykat
Run this file to plot the data generated with master3.py.
Andreas Freise 16.01.2014
--------------------------------------------------------------
""")
# shall we clear the workspace?
# %reset -f
# maybe close all plot windows?
# close('all')
# making these global during testing and debugging
#global kat
#global out
#global result
print("--------------------------------------------------------")
print(" Plotting ASC signals for large misalignments")
asc_large('ITM')
asc_large('ETM')
def asc_large(mir_name):
xscale = 1e6
yscale = 1e6
if mir_name == 'ITM':
ysign = 1.0
else:
ysign = -1.0
tmpfilename = "datashelf_{0}.dat".format(mir_name)
backupname = "datashelf_{0}.dat.bck".format(mir_name)
xscale = 1e6
yscale = 1e6
if mir_name == 'ITM':
ysign = 1.0
else:
ysign = -1.0
tmpfilename = "datashelf_{0}.dat".format(mir_name)
backupname = "datashelf_{0}.dat.bck".format(mir_name)
try:
tmpfile = shelve.open(tmpfilename)
out=tmpfile[str('out')]
maxtems=tmpfile[str('maxtems')]
tmpfile.close()
except: raise Exception("Could not open temprary results file {0}".format(tmpfilename))
try:
#tmpfile = shelve.open(tmpfilename)
#out=tmpfile[str('out')]
#maxtems=tmpfile[str('maxtems')]
#tmpfile.close()
readpickle = pickle.load(open(tmpfilename, "rb"))
out=readpickle['out']
maxtems=readpickle['maxtems']
except: raise Exception("Could not open temprary results file {0}".format(tmpfilename))
fig=pl.figure()
color_cycle=['b', 'c', 'r', 'k']
N=len(maxtems)
lw=np.ones(N)*3
lw[-2]=2
lw[-1]=1
for i, tem in enumerate(maxtems):
data=out[str(tem)]
WFS1_idx=data.ylabels.index("WFS1_I")
WFS2_idx=data.ylabels.index("WFS2_I")
pl.plot(xscale*data.x,ysign*yscale*data.y[:,WFS1_idx],'-', color= color_cycle[i], linewidth=lw[i], label='maxtem {0}'.format(tem))
line, = pl.plot(xscale*data.x,ysign*yscale*data.y[:,WFS2_idx],'-', color = color_cycle[i], linewidth=lw[i])
#line.set_dashes([12, 4])
fig=pl.figure()
color_cycle=['b', 'c', 'r', 'k']
N=len(maxtems)
lw=np.ones(N)*3
lw[-2]=2
lw[-1]=1
for i, tem in enumerate(maxtems):
data=out[str(tem)]
WFS1_idx=data.ylabels.index("WFS1_I")
WFS2_idx=data.ylabels.index("WFS2_I")
pl.plot(xscale*data.x,ysign*yscale*data.y[:,WFS1_idx],'-', color= color_cycle[i], linewidth=lw[i], label='maxtem {0}'.format(tem))
line, = pl.plot(xscale*data.x,ysign*yscale*data.y[:,WFS2_idx],'-', color = color_cycle[i], linewidth=lw[i])
#line.set_dashes([12, 4])
osc1=np.loadtxt("OSCAR_large_tilt_{0}.txt".format(mir_name),comments='%')
osc1=np.loadtxt("OSCAR_large_tilt_{0}.txt".format(mir_name),comments='%')
x=xscale*osc1[:,0]
y=yscale*osc1[:,1]
pl.scatter(x,y,s=80,facecolors='none', edgecolors='k', label='OSCAR')
y=yscale*osc1[:,2]
pl.scatter(x,y,s=80,facecolors='none', edgecolors='k')
pl.xlabel("{0} vertical misalignment [urad]".format(mir_name))
pl.ylabel("Alignment signal [uW]")
if mir_name == 'ITM':
pl.annotate('WFS1',xy=[0.42,70])
pl.annotate('WFS2',xy=[0.62,5])
pl.legend(loc=2)
else:
pl.annotate('WFS1',xy=[0.42,10])
pl.annotate('WFS2',xy=[0.62,-70])
pl.legend(loc=3)
pl.xlim([0,1])
pl.grid()
pl.draw()
pl.show(block=0)
printPDF(fig, "large_{0}.pdf".format(mir_name))
x=xscale*osc1[:,0]
y=yscale*osc1[:,1]
pl.scatter(x,y,s=80,facecolors='none', edgecolors='k', label='OSCAR')
y=yscale*osc1[:,2]
pl.scatter(x,y,s=80,facecolors='none', edgecolors='k')
pl.xlabel("{0} vertical misalignment [urad]".format(mir_name))
pl.ylabel("Alignment signal [uW]")
if mir_name == 'ITM':
pl.annotate('WFS1',xy=[0.42,70])
pl.annotate('WFS2',xy=[0.62,5])
pl.legend(loc=2)
else:
pl.annotate('WFS1',xy=[0.42,10])
pl.annotate('WFS2',xy=[0.62,-70])
pl.legend(loc=3)
pl.xlim([0,1])
pl.grid()
pl.draw()
pl.show(block=0)
printPDF(fig, "large_{0}.pdf".format(mir_name))
if __name__ == '__main__':
main()
main()
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment