detectors.py 5.82 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

Andreas Freise's avatar
Andreas Freise committed
54
    class F:
55
56
57
58
59
60
        def __init__(self, values=None):
            if values is None:
                self.values = []
            else:
                self.values = values

Andreas Freise's avatar
Andreas Freise committed
61
62
63
64
65
        def __len__(self):
            return len(self.values)

        def __getitem__(self, key):
            # if key is of invalid type or value, the list values will raise the error
66
67
            return self.values[key]

Andreas Freise's avatar
Andreas Freise committed
68
69
        def __setitem__(self, key, value):
            print "setting"
70
71
            self.values[key] = SIfloat(value)

Andreas Freise's avatar
Andreas Freise committed
72
73
    
    class Phi():
74
75
76
77
78
79
80
81
82
83
84
85
86
        def __init__(self, values=None):
            print values
            if values is None:
                self.values = []
            else:
                self.values = values

        def __getitem__(self, key): # probably not needed
            print "boom"
            if self.values[key]=="max":
                return self.values[key]
            else:
                return float(self.values[key])
87
            
88
89
90
91
92
        def __setitem__(self,key,value):
            if value=="max":
                self.values[key] = value
            else:
                self.values[key] = SIfloat(value)
Andreas Freise's avatar
Andreas Freise committed
93
94
95
96
97
98
99
100
        def append(self, value):
            self.values.append(value)

    @property
    def f(self): return self.F('f', self.__f)
    @f.setter
    def f(self, key, value): self.__f[key]=value

101
    def __init__(self, name, node, senstype=None, num_demods=0, demods=[]):        
102
        Detector.__init__(self, name, node)
103
104
105
        if num_demods>2:
            raise NotImplementedError("pd with more than two demodulations not implemented yet")   
        self.num_demods = num_demods
106
107
        self.senstype = senstype
        # every second element into f (starting at 1)
108
        
Andreas Freise's avatar
Andreas Freise committed
109
        self.__f = self.F(demods[::2])
110
        
Andreas Freise's avatar
Andreas Freise committed
111
112
        # Every second element into phi (starting at 2)
        self.__phi = self.Phi()
113
        for i in demods[1::2]:
Andreas Freise's avatar
Andreas Freise committed
114
115
116
            self.__phi.append(i)
        print self.__phi
        print self.__phi[0]
117
        
Andreas Freise's avatar
Andreas Freise committed
118

119
    @staticmethod
120
121
122
123
124
    def parseFinesseText(text): 
        values = text.split(" ")

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

153
        if len(values) == 2 * __num_demods + 1 or len(values) == 2 * __num_demods + 2:
154
            return photodiode(values[0], values[-1], __senstype, __num_demods, values[1:len(values)-1])
155
156
157
158
159
        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
160
161
        
    def getFinesseText(self) :
162
163
164
165
        if self.enabled:    
            rtn = []
            
            if self._alternate_beam:
166
                rtn.append("pd {0} {1}".format(self.name, self.node.name))
167
            else:
168
                rtn.append("pd {0} {1}*".format(self.name, self.node.name))
169
170
171
            
            if self.noplot:
                rtn.append("noplot {0}".format(self.name))
Daniel Brown's avatar
Daniel Brown committed
172
            
173
174
175
176
            return rtn
        else:
            return None
    
Daniel Brown's avatar
Daniel Brown committed
177
178
    def getQGraphicsItem(self):
        if self._svgItem == None:
179
            self._svgItem = ComponentQGraphicsItem(":/resources/photodiode_red.svg",self,[(-5,11,self.node)])
Daniel Brown's avatar
Daniel Brown committed
180
181
        
        return self._svgItem    
182