Skip to content
Snippets Groups Projects
Commit fde8c507 authored by Daniel Brown's avatar Daniel Brown
Browse files

removing similarly named objects before adding a new one

parent b89b6d0a
No related branches found
No related tags found
No related merge requests found
...@@ -15,9 +15,12 @@ from collections import namedtuple ...@@ -15,9 +15,12 @@ from collections import namedtuple
from pykat.utilities.optics.gaussian_beams import gauss_param from pykat.utilities.optics.gaussian_beams import gauss_param
class Command(object): class Command(object):
__metaclass__ = abc.ABCMeta
tag = None def __init__(self, name):
__removed = False self.tag = None
self.__removed = False
self.__name = name
def getFinesseText(self): def getFinesseText(self):
""" Base class for individual finesse optical components """ """ Base class for individual finesse optical components """
...@@ -37,28 +40,30 @@ class Command(object): ...@@ -37,28 +40,30 @@ class Command(object):
self._kat.remove(self) self._kat.remove(self)
self.__removed = True self.__removed = True
@property
def name(self): return self.__name
@property @property
def removed(self): return self.__removed def removed(self): return self.__removed
class cavity(Command): class cavity(Command):
def __init__(self, name, c1, n1, c2, n2): def __init__(self, name, c1, n1, c2, n2):
super(Command, self).__init__() Command.__init__(self, axis_type)
self.__name = name
self.__c1 = c1 self.__c1 = c1
self.__c2 = c2 self.__c2 = c2
self.__n1 = n1 self.__n1 = n1
self.__n2 = n2 self.__n2 = n2
def getFinesseText(self): def getFinesseText(self):
return 'cav {0} {1} {2} {3} {4}'.format(self.__name, self.__c1, self.__n1, self.__c2, self.__n2); return 'cav {0} {1} {2} {3} {4}'.format(self.name, self.__c1, self.__n1, self.__c2, self.__n2);
class gauss(object): class gauss(object):
@staticmethod @staticmethod
def parseFinesseText(text, kat): def parseFinesseText(text, kat):
values = text.split() values = text.split()
if not values[0].startswith("gauss") or (len(values) != 6 and len(values) != 8): if not values[0].startswith("gauss") or (len(values) != 6 and len(values) != 8):
raise exceptions.RuntimeError("'{0}' not a valid Finesse gauss command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse gauss command".format(text))
name = values[1] name = values[1]
component = values[2] component = values[2]
...@@ -94,7 +99,7 @@ class tf(Command): ...@@ -94,7 +99,7 @@ class tf(Command):
fQ = namedtuple('fQ', ['f', 'Q']) fQ = namedtuple('fQ', ['f', 'Q'])
def __init__(self, name, poles, zeros): def __init__(self, name, poles, zeros):
super(Command, self).__init__() Command.__init__(self, axis_type)
pass pass
class xaxis(Command): class xaxis(Command):
...@@ -103,9 +108,6 @@ class xaxis(Command): ...@@ -103,9 +108,6 @@ class xaxis(Command):
and interface to the xaxis command in FINESSE. and interface to the xaxis command in FINESSE.
""" """
@property
def name(self): return self._axis_type
def __init__(self, scale, limits, param, steps, comp=None, axis_type="xaxis"): def __init__(self, scale, limits, param, steps, comp=None, axis_type="xaxis"):
""" """
Typical usage: Typical usage:
...@@ -116,7 +118,7 @@ class xaxis(Command): ...@@ -116,7 +118,7 @@ class xaxis(Command):
steps is the number of points to compute between upper and lower limits. steps is the number of points to compute between upper and lower limits.
""" """
super(Command, self).__init__() Command.__init__(self, axis_type)
self._axis_type = axis_type self._axis_type = axis_type
...@@ -129,20 +131,20 @@ class xaxis(Command): ...@@ -129,20 +131,20 @@ class xaxis(Command):
scale = Scale.logarithmic scale = Scale.logarithmic
elif isinstance(scale, str): elif isinstance(scale, str):
# else we have a string but not a recognisable one # else we have a string but not a recognisable one
raise exceptions.ValueError("scale argument '{0}' is not valid, must be 'lin' or 'log'".format(scale)) raise pkex.BasePyKatException("scale argument '{0}' is not valid, must be 'lin' or 'log'".format(scale))
if scale != Scale.linear and scale != Scale.logarithmic: if scale != Scale.linear and scale != Scale.logarithmic:
raise exceptions.ValueError("scale is not Scale.linear or Scale.logarithmic") raise pkex.BasePyKatException("scale is not Scale.linear or Scale.logarithmic")
self.scale = scale self.scale = scale
if numpy.size(limits) != 2 : if numpy.size(limits) != 2 :
raise exceptions.ValueError("limits input should be a 2x1 vector of limits for the xaxis") raise pkex.BasePyKatException("limits input should be a 2x1 vector of limits for the xaxis")
self.limits = numpy.array(SIfloat(limits)).astype(float) self.limits = numpy.array(SIfloat(limits)).astype(float)
if steps <= 0 : if steps <= 0 :
raise exceptions.ValueError("steps value should be > 0") raise pkex.BasePyKatException("steps value should be > 0")
self.steps = int(steps) self.steps = int(steps)
...@@ -173,14 +175,14 @@ class xaxis(Command): ...@@ -173,14 +175,14 @@ class xaxis(Command):
values = text.split() values = text.split()
if values[0] != "xaxis" and values[0] != "xaxis*": if values[0] != "xaxis" and values[0] != "xaxis*":
raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse xaxis command".format(text))
axis_type = values[0] axis_type = values[0]
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) != 6: if len(values) != 6:
raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("xaxis Finesse code format incorrect '{0}'".format(text))
return xaxis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0], axis_type=axis_type) return xaxis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0], axis_type=axis_type)
...@@ -204,13 +206,13 @@ class x2axis(xaxis): ...@@ -204,13 +206,13 @@ class x2axis(xaxis):
values = text.split() values = text.split()
if values[0] != "x2axis" and values[0] != "x2axis*": if values[0] != "x2axis" and values[0] != "x2axis*":
raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse xaxis command".format(text))
axis_type = values[0] axis_type = values[0]
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) != 6: if len(values) != 6:
raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("xaxis Finesse code format incorrect '{0}'".format(text))
return x2axis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0]) return x2axis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0])
...@@ -549,8 +549,13 @@ class kat(object): ...@@ -549,8 +549,13 @@ class kat(object):
self.__blocks[self.__currentTag].contents.append(line) self.__blocks[self.__currentTag].contents.append(line)
if obj != None and not isinstance(obj, str): if obj != None and not isinstance(obj, str):
if self.hasNamedObject(obj.name):
getattr(self, obj.name).remove()
print "Removed existing object '{0}' of type {1} to add new object".format(obj.name, obj.__class__)
self.add(obj) self.add(obj)
# now process all the varous gauss/attr etc. commands which require # now process all the varous gauss/attr etc. commands which require
# components to exist first before they can be processed # components to exist first before they can be processed
for line in after_process: for line in after_process:
...@@ -835,6 +840,9 @@ class kat(object): ...@@ -835,6 +840,9 @@ class kat(object):
import gc import gc
print gc.get_referrers(obj) print gc.get_referrers(obj)
def hasNamedObject(self, name):
return name in self.__components or name in self.__detectors or name in self.__commands
def add(self, obj): def add(self, obj):
try: try:
obj.tag = self.__currentTag obj.tag = self.__currentTag
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment