detectors.py 6.1 KB
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 01 09:09:10 2013

@author: Daniel
"""
import exceptions
import pykat.gui.resources

from pykat.utils import *
from pykat.gui.graphics import *
from pykat.node_network import *

14
class Detector(object) :
15
    def __init__(self, name,node):
Daniel Brown's avatar
Daniel Brown committed
16
17
        self.__name = name
        self._svgItem = None
18
        self._kat = None
19
20
        self.noplot = False
        self.enabled = True
Daniel Brown's avatar
Daniel Brown committed
21
        self.tag = None
22
        self.__node = None
Daniel Brown's avatar
Daniel Brown committed
23
        
24
25
26
        if node.find('*'):
            self._alternate_beam = True
            node.replace('*','')
Daniel Brown's avatar
Daniel Brown committed
27
        
28
29
30
        self.__requested_node = node

    def _on_kat_add(self, kat):
31
        self.__node = kat.nodes.createNode(self.__requested_node)
32
33
34
35
    
    @staticmethod
    def parseFinesseText(text):    
        raise NotImplementedError("This function is not implemented")
Daniel Brown's avatar
Daniel Brown committed
36
37
38
39
40
41
42
43
        
    def getFinesseText(self):
        """ Base class for individual finesse optical components """    
        raise NotImplementedError("This function is not implemented")
        
    def getQGraphicsItem(self):    
        return None
    
44
45
    @property 
    def node(self): return self.__node
Daniel Brown's avatar
Daniel Brown committed
46
47
48
49
50
51
52
        
    def __getname(self):
        return self.__name        
        
    name = property(__getname)

class photodiode(Detector):
53

54
    class __F(list):
55
        def __init__(self, values=None):
56
57
58
            if values==None:
                values = []
            list.__init__(self,[SIfloat(value) for value in values])
59
60

        """    
Andreas Freise's avatar
Andreas Freise committed
61
        def __setitem__(self, key, value):
62
            list.__setitem__(self,key, SIfloat(value))
63

64
65
        def append(self, value):
            list.append(self,SIfloat(value))
66
        """    
67
68
69
70
71
            
    class __Phi(list):
        def __convertValue(self, value):
            if value=="max":
                return value                
72
            else:
73
74
75
76
77
78
                return SIfloat(value)
                
        def __init__(self, values=None):
            if values==None:
                values = []
            list.__init__(self,[self.__convertValue(value) for value in values])
79
80

        def __getitem__(self, key): # probably not needed
81
82
            if list.__getitem__(self,key)=="max":
                return list.__getitem__(self,key)
83
            else:
84
                return float(list.__getitem__(self,key))
85
        """    
86
        def __setitem__(self,key,value):
87
            list.__setitem__(self,key, self.__convertValue(value))
Andreas Freise's avatar
Andreas Freise committed
88

89
90
        def append(self, value):
            list.append(self,self.__convertValue(value))
91
        """
92
            
Andreas Freise's avatar
Andreas Freise committed
93
    @property
94
    def f(self): return self.__f
Andreas Freise's avatar
Andreas Freise committed
95

96
97
98
99
    @property
    def phi(self): return self.__phi

        
100
    def __init__(self, name, node, senstype=None, num_demods=0, demods=[]):        
101
        Detector.__init__(self, name, node)
102
103
104
        if num_demods>2:
            raise NotImplementedError("pd with more than two demodulations not implemented yet")   
        self.num_demods = num_demods
105
        self.senstype = senstype
Andreas Freise's avatar
Andreas Freise committed
106

107
        # every second element into f (starting at 1)
108
        self.__f = self.__F(demods[::2])
109
        
Andreas Freise's avatar
Andreas Freise committed
110
        # Every second element into phi (starting at 2)
Andreas Freise's avatar
Andreas Freise committed
111
112
        self.__phi = self.__Phi(demods[1::2])
        """
113
        for i in demods[1::2]:
114
            self.__phi.append(i)        
Andreas Freise's avatar
Andreas Freise committed
115
116
        """
        
117
    @staticmethod
118
119
120
121
122
    def parseFinesseText(text): 
        values = text.split(" ")

        if values[0][0:2] != "pd":
            raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
123
        if len(values[0])==2:
124
            __num_demods=0
125
126
127
128
129
130
            __senstype=""
        elif len(values[0])==3 or len(values[0])==4:
            if values[0][2]=="S":
                __senstype="S"
            elif values[0][2]=="N":
                __senstype="N"
131
132
            else:
                try:
133
134
                    __num_demods=int(values[0][2])
                    __senstype=""
135
136
                except ValueError:
                    raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
137
            if len(values[0])==4:
138
                try:
139
                    __num_demods=int(values[0][3])
140
141
142
143
144
145
146
147
148
                except ValueError:
                    raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))                
        else:
            raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))

        if __num_demods<0 or __num_demods>5:
            raise exceptions.FinesseParse("'{0}' number of demodulations must be >0 and <5".format(text))

        values.pop(0) # remove initial value
149

150
        if len(values) == 2 * __num_demods + 1 or len(values) == 2 * __num_demods + 2:
151
            return photodiode(values[0], values[-1], __senstype, __num_demods, values[1:len(values)-1])
152
153
154
155
156
        else:
            raise exceptions.FinesseParse("Photodiode code format incorrect '{0}'".format(text))

        #return photodiode("name", "node", demods)   
        #raise NotImplementedError("This function is not implemented")   
Daniel Brown's avatar
Daniel Brown committed
157
158
        
    def getFinesseText(self) :
159
        if self.enabled:
160
            rtn = []
161
162
163
            __f_phi=range(len(self.f)+len(self.phi))
            __f_phi[::2]=self.f
            __f_phi[1::2]=self.phi
164
            __f_phi_str = " ".join(map(str, __f_phi))
165
166
            
            if self._alternate_beam:
167
                rtn.append("pd{0}{1} {2} {3} {4}".format(self.senstype, self.num_demods, self.name, __f_phi_str,  self.node.name))
168
            else:
169
                rtn.append("pd{0}{1} {2} {3} {4}*".format(self.senstype, self.num_demods, self.name, __f_phi_str,  self.node.name))
170
171
172
            
            if self.noplot:
                rtn.append("noplot {0}".format(self.name))
Daniel Brown's avatar
Daniel Brown committed
173
            
174
175
176
177
            return rtn
        else:
            return None
    
Daniel Brown's avatar
Daniel Brown committed
178
179
    def getQGraphicsItem(self):
        if self._svgItem == None:
180
            self._svgItem = ComponentQGraphicsItem(":/resources/photodiode_red.svg",self,[(-5,11,self.node)])
Daniel Brown's avatar
Daniel Brown committed
181
182
        
        return self._svgItem    
183