detectors.py 5.43 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    class f(list):
        def __init__(self, values=None):
            print "tadaaaaaaaaaaaaaaa"
            if values is None:
                self.values = []
            else:
                self.values = values

        def __getitem__(self,key):
            return self.values[key]

        def __setitem__(self,key,value):
            self.values[key] = SIfloat(value)

    class phi(list):
        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])
82
            
83
84
85
86
87
88
89
        def __setitem__(self,key,value):
            if value=="max":
                self.values[key] = value
            else:
                self.values[key] = SIfloat(value)
                
    
90
    def __init__(self, name, node, senstype=None, num_demods=0, demods=[]):        
91
        Detector.__init__(self, name, node)
92
93
94
        if num_demods>2:
            raise NotImplementedError("pd with more than two demodulations not implemented yet")   
        self.num_demods = num_demods
95
96
97
        self.senstype = senstype
        # every second element into f (starting at 1)
        self.f(demods[::2])
98
        
99
100
        # every second element into phi (starting at 2)
        self.phi([1,2])
101
        
102
103
104
        for i in demods[1::2]:
            self.phi.append(i)
        
105
    @staticmethod
106
107
108
109
110
    def parseFinesseText(text): 
        values = text.split(" ")

        if values[0][0:2] != "pd":
            raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
111
        if len(values[0])==2:
112
            __num_demods=0
113
114
115
116
117
118
119
            __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"
120
121
            else:
                try:
122
123
                    __num_demods=int(values[0][2])
                    __senstype=""
124
125
                except ValueError:
                    raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
126
            if len(values[0])==4:
127
                try:
128
                    __num_demods=int(values[0][3])
129
130
131
132
133
134
135
136
137
                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
138

139
        if len(values) == 2 * __num_demods + 1 or len(values) == 2 * __num_demods + 2:
140
            return photodiode(values[0], values[-1], __senstype, __num_demods, values[1:len(values)-1])
141
142
143
144
145
        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
146
147
        
    def getFinesseText(self) :
148
149
150
151
        if self.enabled:    
            rtn = []
            
            if self._alternate_beam:
152
                rtn.append("pd {0} {1}".format(self.name, self.node.name))
153
            else:
154
                rtn.append("pd {0} {1}*".format(self.name, self.node.name))
155
156
157
            
            if self.noplot:
                rtn.append("noplot {0}".format(self.name))
Daniel Brown's avatar
Daniel Brown committed
158
            
159
160
161
162
            return rtn
        else:
            return None
    
Daniel Brown's avatar
Daniel Brown committed
163
164
    def getQGraphicsItem(self):
        if self._svgItem == None:
165
            self._svgItem = ComponentQGraphicsItem(":/resources/photodiode_red.svg",self,[(-5,11,self.node)])
Daniel Brown's avatar
Daniel Brown committed
166
167
        
        return self._svgItem    
168