00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2002
00004 # by
00005 # The Board of Trustees of the
00006 # Leland Stanford Junior University.
00007 # All rights reserved.
00008 #
00009
00010 __facility__ = "Online"
00011 __abstract__ = "Classes for handling raw/engineering unit (EGU) oconversions"
00012 __author__ = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__ = ("$Date: 2005/04/13 03:28:48 $").split(' ')[1]
00014 __version__ = "$Revision: 2.4 $"
00015 __release__ = "$Name: R04-12-00 $"
00016 __credits__ = "SLAC"
00017
00018 import LATTE.copyright_SLAC
00019 from math import *
00020
00021
00022 class GEGU(object):
00023 """\brief Base class for raw/engineering unit conversions.
00024
00025 """
00026 def __init__(self, units):
00027 self.__units = units
00028
00029 def egu(self, value):
00030 """Overloadable method for converting raw units to engineering units
00031
00032 \param value Value in raw units.
00033
00034 \return Engineering units value after the conversion.
00035
00036 """
00037 return value
00038
00039 def raw(self, value):
00040 """Overloadable method for converting engineering units to raw units
00041
00042 \param value Value in engineering units.
00043
00044 \return Raw value after the conversion.
00045
00046 """
00047 return value
00048
00049 def units(self):
00050 """Method for returning the units string
00051
00052 \return Units string value.
00053
00054 """
00055 return self.__units
00056
00057
00058 class GEGU_linear(GEGU):
00059 """\brief EGU class for performing linear conversions.
00060
00061 """
00062 def __init__(self, slope, intercept, units=""):
00063 """Initialize a raw/EGU linear conversion object.
00064
00065 \param slope Number of engineering unit counts per raw value tick
00066 \param intercept Offset in units of the engineering unit
00067 \param units The engineering units name, e.g. 'cm'
00068
00069 """
00070 assert slope, "GEGU_linear.__init__: slope argument cannot be zero"
00071 GEGU.__init__(self, units)
00072 self.__slope = slope
00073 self.__intercept = intercept
00074
00075 def egu(self, rawValue):
00076 """Convert a value in raw units to engineering units
00077
00078 \param rawValue Value in raw units.
00079
00080 \return Engineering units value after the conversion.
00081
00082 """
00083
00084 # Compensate for some pseudo quantities (like PSD_TEM_0)
00085 # where the rawValue may be received as a large positive
00086 # value although it is negative.
00087 if rawValue >= (1 << 31L):
00088 rawValue = rawValue - (1 << 32L)
00089
00090 return rawValue * self.__slope + self.__intercept
00091
00092 def raw(self, eguValue):
00093 """Convert a value in engineering units to raw units
00094
00095 \param eguValue Value in engineering units.
00096
00097 \return Raw value after the conversion.
00098
00099 """
00100 return int(round((eguValue - self.__intercept) / self.__slope))
00101
00102 class GEGU_poly3(GEGU):
00103 """\brief EGU class for performing 3rd degree polynominal conversions.
00104
00105 """
00106 def __init__(self, coeff0, coeff1, coeff2, coeff3, offset, units=""):
00107 """Initialize a raw/EGU linear conversion object.
00108
00109 \param slope Number of engineering unit counts per raw value tick
00110 \param intercept Offset in units of the engineering unit
00111 \param units The engineering units name, e.g. 'cm'
00112
00113 """
00114 # assert slope, "GEGU_linear.__init__: slope argument cannot be zero"
00115 GEGU.__init__(self, units)
00116 self.__coeff0 = coeff0
00117 self.__coeff1 = coeff1
00118 self.__coeff2 = coeff2
00119 self.__coeff3 = coeff3
00120 self.__offset = offset
00121
00122 def egu(self, rawValue):
00123 """Convert a value in raw units to engineering units
00124
00125 \param rawValue Value in raw units
00126
00127 \return Engineering units value after the conversion.
00128
00129 """
00130 return self.__coeff0 + self.__coeff1*(rawValue-self.__offset) + self.__coeff2*(rawValue-self.__offset)**2 + self.__coeff3*(rawValue-self.__offset)**3
00131
00132 def raw(self, eguValue):
00133 """Convert a value in engineering units to raw units
00134
00135 \param eguValue Value in engineering units.
00136
00137 \return Raw value after the conversion.
00138
00139 """
00140 return 0
00141
00142
00143 class GEGU_thermistor(GEGU):
00144 """\brief EGU class for performing thermistor conversions.
00145
00146 Actual equation is: R = (C1 / Counts) + C0
00147
00148 Temp (deg C) = C2 * ln(R) + C3
00149
00150 Nominal C0 = - 1.020 E5
00151 Nominal C1 = 4.096 E8
00152
00153 Fitted from (-40,80 degree) thermistor data from YSI. 30K ohm column (http:
00154 Fitted C2 = - 20.319
00155 Fitted C3 = 234.69
00156
00157
00158 """
00159 def __init__(self, coeff0, coeff1, coeff2, coeff3, units=""):
00160 """Initialize a raw/EGU conversion object.
00161
00162 \param coeff[0-3] One of the coefficients of the function. See function doc
00163 \param units The engineering units name, e.g. 'cm'
00164
00165 """
00166 GEGU.__init__(self, units)
00167 self.__coeff0 = coeff0
00168 self.__coeff1 = coeff1
00169 self.__coeff2 = coeff2
00170 self.__coeff3 = coeff3
00171
00172 def egu(self, rawValue):
00173 """Convert a value in raw units to engineering units
00174
00175 \param rawValue Value in raw units
00176
00177 \return Engineering units value after the conversion.
00178
00179 """
00180 if rawValue == 0: # protect divide by zero
00181 rawValue = 1
00182 R = (self.__coeff1 / rawValue) + self.__coeff0
00183 T = (self.__coeff2 * log(R)) + self.__coeff3
00184 return T
00185
00186 def raw(self, eguValue):
00187 """Convert a value in engineering units to raw units
00188
00189 \param eguValue Value in engineering units.
00190
00191 \return Raw value after the conversion.
00192
00193 """
00194 return 0
00195
00196
00197 class GEGU_rtd(GEGU):
00198 """\brief EGU class for performing RTD (Resistive Temp Device) conversions.
00199
00200 Actual equation is:
00201
00202 C = ( R/(R+C0) - 1/(C2) ) * C1
00203
00204 C0 (C C2 + C1)
00205 R = - -----------------
00206 C C2 - C1 C2 + C1
00207
00208 Temp = ( R + C4 ) / C5
00209
00210 Nominal C0 = 1820
00211 Nominal C1 = 4096 (Exact)
00212 Nominal C2 = 3
00213
00214 Fitted from +/- 100 degree data from 0118MF literature from Goodrich (manufacturer)
00215 Fitted C3 = - 1994.5
00216 Fitted C4 = 7.9051
00217
00218 """
00219 def __init__(self, coeff0, coeff1, coeff2, coeff3, coeff4, units=""):
00220 """Initialize a raw/EGU linear conversion object.
00221
00222 \param coeff[0-4] One of the coefficients of the function. See function doc
00223 \param units The engineering units name, e.g. 'cm'
00224
00225 """
00226 GEGU.__init__(self, units)
00227 self.__coeff0 = coeff0
00228 self.__coeff1 = coeff1
00229 self.__coeff2 = coeff2
00230 self.__coeff3 = coeff3
00231 self.__coeff4 = coeff4
00232
00233 def egu(self, rawValue):
00234 """Convert a value in raw units to engineering units
00235
00236 \param rawValue Value in raw units
00237
00238 \return Engineering units value after the conversion.
00239
00240 """
00241 R = - self.__coeff0 * \
00242 ( ((rawValue*self.__coeff2) + self.__coeff1) / \
00243 ((rawValue*self.__coeff2) + self.__coeff1*(1-self.__coeff2)) )
00244 T = ( R + self.__coeff3 ) / self.__coeff4
00245 return T
00246
00247 def raw(self, eguValue):
00248 """Convert a value in engineering units to raw units
00249
00250 \param eguValue Value in engineering units.
00251
00252 \return Raw value after the conversion.
00253
00254 """
00255 return 0
00256
00257