''' Copyright 2010 Expert Sleepers. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY EXPERT SLEEPERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EXPERT SLEEPERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Expert Sleepers. ''' ''' Calibration data reading/writing class. v1.0.0 (14/1/2010) v1.0.1 (3/3/2010) - removed use of 'with' and 'format' for Python 2.5 compatibility ''' import sys class CalibrationData: def __init__(self): self.clear() def clear(self): self._values = [0.0] * 129 self._valid = [False] * 129 def value(self, index): if index < 0 or index >= 129: raise RuntimeError( 'index %d out of range' % index ) if isinstance( index, int ): return self._values[ index ] i = int( index ) f = index - float( i ) v0 = self._values[ i ] v1 = self._values[ i+1 ] return v0 + f * ( v1 - v0 ) def dump(self): print self._values print self._valid for i, v in enumerate( self._valid ): if v: print '%d: %f' % ( i, self._values[i] ) def load(self, filename): self.clear() file = open( filename, 'r' ) lines = file.readlines() file.close() if lines[0] != 'Silent Way Calibration Data\n': raise RuntimeError( 'failed to read header' ) if lines[1] != 'version: 1\n': raise RuntimeError( 'failed to read version' ) for line in lines[2:]: bits = line[:-1].split( ' ' ) index = int( bits[0] ) value = float( bits[1] ) if index < 0 or index >= 129: raise RuntimeError( 'index %d out of range' % index ) if abs( value ) > 1.0: raise RuntimeError( 'value %f out of range' % value ) self._valid[ index ] = True self._values[ index ] = value # fill in gaps valid = [ i for i, v in enumerate( self._valid ) if v ] firstValid = valid[0] lastValid = valid[-1] for i in range( firstValid, lastValid+1 ): if not self._valid[ i ]: low = i-1 high = i+1 while not self._valid[low]: low -= 1 while not self._valid[high]: high += 1 v0 = self._values[ low ] v1 = self._values[ high ] self._values[ i ] = v0 + ( v1 - v0 ) * ( i - low )/float( high - low ) def save(self, filename): file = open( filename, 'w' ) file.write( 'Silent Way Calibration Data\n' ) file.write( 'version: 1\n' ) for i in range( 0, 129 ): if self._valid[ i ]: file.write( '%d %.8f\n' % ( i, self._values[ i ] ) ) file.close() if __name__ == '__main__': # simple test - load and save a file cd = CalibrationData() cd.load( sys.argv[1] ) cd.dump() print cd.value( 15 ) print cd.value( 10.5 ) cd.save( sys.argv[2] )