commands.py 5.65 KB
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 11:58:09 2013

@author: Daniel
"""
import numpy
from numpy import min,max
import exceptions
from components import *
from structs import *

13
class Command(object):
Daniel Brown's avatar
Daniel Brown committed
14
15
16
    def __init__(self):
        self.tag = None
        
Daniel Brown's avatar
Daniel Brown committed
17
18
19
    def getFinesseText(self):
        """ Base class for individual finesse optical components """    
        raise NotImplementedError("This function is not implemented")
20
    
21
22
23
24
    @staticmethod
    def parseFinesseText(text):    
        raise NotImplementedError("This function is not implemented")
    
25
26
27
28
29
    def _on_kat_add(self, kat):
        """
        Called when this component has been added to a kat object
        """
        self._kat = kat
Daniel Brown's avatar
Daniel Brown committed
30
        
31
class cavity(Command):
32
    def __init__(self, name, c1, n1, c2, n2):
33
34
35
36
37
38
39
        self.__name = name
        self.__c1 = c1
        self.__c2 = c2
        self.__n1 = n1
        self.__n2 = n2
        
    def getFinesseText(self):
40
        return 'cav {0} {1} {2} {3} {4}'.format(self.__name, self.__c1, self.__n1, self.__c2, self.__n2);
41

42
43
44
45
46
47
48
49
50
51
class gauss(object):
    @staticmethod
    def parseFinesseText(text, kat):  
        values = text.split(" ")
        
        if not values[0].startswith("gauss"):
            raise exceptions.RuntimeError("'{0}' not a valid Finesse gauss command".format(text))
        
        
        
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class attr():
    @staticmethod
    def parseFinesseText(text, kat):  
        values = text.split(" ")
        
        if values[0] != "attr":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse attr command".format(text))

        values.pop(0) # remove initial value
        
        if len(values) < 3:
            raise exceptions.RuntimeError("attr Finesse code format incorrect '{0}'".format(text))
        
        comp = None
        comp_name = values[0]
        values.pop(0)
        
        for c in kat.getComponents():
            if c.name == comp_name:
                comp = c
                break
        
        if comp == None:
            raise 
        # can list multiple attributes per command
77
       
Daniel Brown's avatar
Daniel Brown committed
78
79
class xaxis(Command):
    
80
81
    def __init__(self, scale, limits, comp, param, steps, axis_type="xaxis"):
        self._axis_type = axis_type
Daniel Brown's avatar
Daniel Brown committed
82
        
83
84
85
86
87
88
89
90
        if scale == "lin":
            scale = Scale.linear
        elif scale == "log":
            scale = Scale.logarithmic
        elif isinstance(scale, str): 
            # 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))
         
Daniel Brown's avatar
Daniel Brown committed
91
92
93
94
95
96
97
98
        if scale != Scale.linear and scale != Scale.logarithmic:
            raise exceptions.ValueError("scale is not Scale.linear or Scale.logarithmic")            
            
        self.scale = scale
        
        if numpy.size(limits) != 2 :
            raise exceptions.ValueError("limits input should be a 2x1 vector of limits for the xaxis")
            
99
        self.limits = numpy.array(SIfloat(limits)).astype(float)
Daniel Brown's avatar
Daniel Brown committed
100
101
102
103
        
        if steps <= 0 :
            raise exceptions.ValueError("steps value should be > 0")            
            
104
        self.steps = int(steps)
Daniel Brown's avatar
Daniel Brown committed
105
        
106
107
108
109
110
111
112
        # if entered component is a string then just store and use that
        if isinstance(comp, str):
            self.__comp = comp
        elif not isinstance(comp, Component):
            raise exceptions.ValueError("{0} has not been added".format(comp))
        else:
            self.__comp = comp
Daniel Brown's avatar
Daniel Brown committed
113
        
114
115
116
        if isinstance(param, str):
            self.__param = param
        elif not isinstance(param, Param) :
Daniel Brown's avatar
Daniel Brown committed
117
            raise exceptions.ValueError("param argument is not of type Param")
118
119
        else:
            self.__param = param
120
121
122
123
124
    
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")
        
125
        if values[0] != "xaxis" and values[0] != "xaxis*":
126
            raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text))
127
128
129
        
        axis_type = values[0]
        
130
131
132
133
134
        values.pop(0) # remove initial value
        
        if len(values) != 6:
            raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text))

135
        return xaxis(values[2], [values[3], values[4]], values[0], values[1], values[5], axis_type=axis_type)
Daniel Brown's avatar
Daniel Brown committed
136
137
        
    def getFinesseText(self):
138
139
140
        # store either the component name of the string provided
        comp_name = self.__comp.name if isinstance(self.__comp, Component) else self.__comp
        param_name = self.__param.name if isinstance(self.__param, Param) else self.__param
Daniel Brown's avatar
Daniel Brown committed
141
        
142
        return '{axis_type} {0} {1} {2} {3} {4} {5}'.format(
143
                comp_name, param_name, self.scale,
144
                min(self.limits), max(self.limits), self.steps, axis_type=self._axis_type);
Daniel Brown's avatar
Daniel Brown committed
145
                
146
147
148
149
150
151
152
153
154
155
class x2axis(xaxis):
    def __init__(self, scale, limits, comp, param, steps):
        xaxis.__init__(self, scale, limits, comp, param, steps, axis_type="x2axis")        

    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")
        
        if values[0] != "x2axis" and values[0] != "x2axis*":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text))
156
        
157
158
159
160
161
162
163
164
        axis_type = values[0]
        
        values.pop(0) # remove initial value
        
        if len(values) != 6:
            raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text))

        return x2axis(values[2], [values[3], values[4]], values[0], values[1], values[5])