detectors.py 5.56 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
46
47
48
    
    @property
    def name(self): return self.__name        
Daniel Brown's avatar
Daniel Brown committed
49

50
51
    def __str__(self): return self.name
    
Daniel Brown's avatar
Daniel Brown committed
52
class photodiode(Detector):
53

54
    class __F(list):
55
        def __init__(self, values=None):
56
57
58
59
60
61
62
63
            if values==None:
                values = []
            list.__init__(self,[SIfloat(value) for value in values])
            
    class __Phi(list):
        def __convertValue(self, value):
            if value=="max":
                return value                
64
            else:
65
66
67
68
69
70
                return SIfloat(value)
                
        def __init__(self, values=None):
            if values==None:
                values = []
            list.__init__(self,[self.__convertValue(value) for value in values])
71
72

        def __getitem__(self, key): # probably not needed
73
74
            if list.__getitem__(self,key)=="max":
                return list.__getitem__(self,key)
75
            else:
76
77
                return float(list.__getitem__(self,key))
            
Andreas Freise's avatar
Andreas Freise committed
78
    @property
79
    def f(self): return self.__f
Andreas Freise's avatar
Andreas Freise committed
80

81
82
83
    @property
    def phi(self): return self.__phi
        
84
    def __init__(self, name, node, senstype="", num_demods=0, demods=[]):        
85
        Detector.__init__(self, name, node)
86
87
88
        if num_demods>2:
            raise NotImplementedError("pd with more than two demodulations not implemented yet")   
        self.num_demods = num_demods
89
        self.senstype = senstype
Andreas Freise's avatar
Andreas Freise committed
90

91
        # every second element into f (starting at 1)
92
        self.__f = self.__F(demods[::2])
93
        
Andreas Freise's avatar
Andreas Freise committed
94
        # Every second element into phi (starting at 2)
Andreas Freise's avatar
Andreas Freise committed
95
96
        self.__phi = self.__Phi(demods[1::2])
        
97
    @staticmethod
98
99
100
101
102
    def parseFinesseText(text): 
        values = text.split(" ")

        if values[0][0:2] != "pd":
            raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
103
        if len(values[0])==2:
104
            __num_demods=0
105
106
107
108
109
110
            __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"
111
112
            else:
                try:
113
114
                    __num_demods=int(values[0][2])
                    __senstype=""
115
116
                except ValueError:
                    raise exceptions.FinesseParse("'{0}' not a valid photodiode command".format(text))
117
            if len(values[0])==4:
118
                try:
119
                    __num_demods=int(values[0][3])
120
121
122
123
124
125
126
127
128
                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
129

130
        if len(values) == 2 * __num_demods + 1 or len(values) == 2 * __num_demods + 2:
131
            return photodiode(values[0], values[-1], __senstype, __num_demods, values[1:len(values)-1])
132
133
134
135
136
        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
137
138
        
    def getFinesseText(self) :
139
        if self.enabled:
140
            rtn = []
141
142
143
            __f_phi=range(len(self.f)+len(self.phi))
            __f_phi[::2]=self.f
            __f_phi[1::2]=self.phi
144
            __f_phi_str = " ".join(map(str, __f_phi))
145
146
            
            if self._alternate_beam:
147
                rtn.append("pd{0}{1} {2} {3} {4}".format(self.senstype, self.num_demods, self.name, __f_phi_str,  self.node.name))
148
            else:
149
                rtn.append("pd{0}{1} {2} {3} {4}*".format(self.senstype, self.num_demods, self.name, __f_phi_str,  self.node.name))
150
151
152
            
            if self.noplot:
                rtn.append("noplot {0}".format(self.name))
Daniel Brown's avatar
Daniel Brown committed
153
            
154
155
156
157
            return rtn
        else:
            return None
    
Daniel Brown's avatar
Daniel Brown committed
158
159
    def getQGraphicsItem(self):
        if self._svgItem == None:
160
            self._svgItem = ComponentQGraphicsItem(":/resources/photodiode_red.svg",self,[(-5,11,self.node)])
Daniel Brown's avatar
Daniel Brown committed
161
162
        
        return self._svgItem    
163