[13339] | 1 | #! /usr/bin/env python
|
---|
| 2 |
|
---|
| 3 | import os
|
---|
| 4 | import sys
|
---|
| 5 | import numpy
|
---|
| 6 | import math
|
---|
| 7 | import struct
|
---|
[20097] | 8 | import argparse
|
---|
[13339] | 9 |
|
---|
[20892] | 10 | def BinRead(filin,filout='',verbose=0): #{{{
|
---|
[13339] | 11 |
|
---|
| 12 | print "reading binary file."
|
---|
| 13 | f=open(filin,'rb')
|
---|
| 14 |
|
---|
| 15 | if filout:
|
---|
| 16 | sys.stdout=open(filout,'w')
|
---|
| 17 |
|
---|
| 18 | while True:
|
---|
| 19 | try:
|
---|
[20890] | 20 | #Step 1: read size of record name
|
---|
| 21 | recordnamesize=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[13339] | 22 | except struct.error as e:
|
---|
| 23 | print "probable EOF: %s" % e
|
---|
| 24 | break
|
---|
[20892] | 25 |
|
---|
| 26 | print "============================================================================"
|
---|
| 27 | if verbose>2:
|
---|
| 28 | print "\nrecordnamesize = \"%d\"" % (recordnamesize)
|
---|
[20890] | 29 | recordname=struct.unpack('%ds' % recordnamesize,f.read(recordnamesize))[0]
|
---|
[20892] | 30 | print "field: %s" % recordname
|
---|
[13339] | 31 |
|
---|
| 32 | #Step 2: read the data itself.
|
---|
| 33 | #first read length of record
|
---|
| 34 | reclen=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 35 | if verbose>1:
|
---|
| 36 | print "reclen = %d" % reclen
|
---|
[13339] | 37 |
|
---|
| 38 | #read data code:
|
---|
| 39 | code=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 40 | #print "code = %d (%s)" % (code,CodeToFormat(code))
|
---|
| 41 | print "Format = %s" % CodeToFormat(code)
|
---|
[13339] | 42 |
|
---|
| 43 | if code == FormatToCode('Boolean'):
|
---|
| 44 | # bval=struct.unpack('b',f.read(reclen-struct.calcsize('i')))[0]
|
---|
| 45 | bval=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0]
|
---|
[20892] | 46 | print "value = %d" % bval
|
---|
[13339] | 47 |
|
---|
| 48 | elif code == FormatToCode('Integer'):
|
---|
| 49 | ival=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0]
|
---|
[20892] | 50 | print "value = %d" % ival
|
---|
[13339] | 51 |
|
---|
| 52 | elif code == FormatToCode('Double'):
|
---|
| 53 | dval=struct.unpack('d',f.read(reclen-struct.calcsize('i')))[0]
|
---|
[20892] | 54 | print "value = %f" % dval
|
---|
[13339] | 55 |
|
---|
| 56 | elif code == FormatToCode('String'):
|
---|
| 57 | strlen=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 58 | if verbose>1:
|
---|
| 59 | print "strlen = %d" % strlen
|
---|
[13339] | 60 | sval=struct.unpack('%ds' % strlen,f.read(strlen))[0]
|
---|
[20892] | 61 | print "value = '%s'" % sval
|
---|
[13339] | 62 |
|
---|
| 63 | elif code == FormatToCode('BooleanMat'):
|
---|
| 64 | #read matrix type:
|
---|
| 65 | mattype=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 66 | print "mattype = %d" % mattype
|
---|
| 67 |
|
---|
| 68 | #now read matrix
|
---|
| 69 | s=[0,0]
|
---|
| 70 | s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 71 | s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 72 | print "size = [%dx%d]" % (s[0],s[1])
|
---|
[13339] | 73 | data=numpy.zeros((s[0],s[1]))
|
---|
| 74 | for i in xrange(s[0]):
|
---|
| 75 | for j in xrange(s[1]):
|
---|
| 76 | data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose
|
---|
[20892] | 77 | if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j])
|
---|
[13339] | 78 |
|
---|
| 79 | elif code == FormatToCode('IntMat'):
|
---|
| 80 | #read matrix type:
|
---|
| 81 | mattype=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 82 | print "mattype = %d" % mattype
|
---|
| 83 |
|
---|
| 84 | #now read matrix
|
---|
| 85 | s=[0,0]
|
---|
| 86 | s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 87 | s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 88 | print "size = [%dx%d]" % (s[0],s[1])
|
---|
[13339] | 89 | data=numpy.zeros((s[0],s[1]))
|
---|
| 90 | for i in xrange(s[0]):
|
---|
| 91 | for j in xrange(s[1]):
|
---|
| 92 | data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose
|
---|
[20892] | 93 | if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j])
|
---|
[13339] | 94 |
|
---|
| 95 | elif code == FormatToCode('DoubleMat'):
|
---|
| 96 | #read matrix type:
|
---|
| 97 | mattype=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 98 | print "mattype = %d" % mattype
|
---|
| 99 |
|
---|
| 100 | #now read matrix
|
---|
| 101 | s=[0,0]
|
---|
| 102 | s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
| 103 | s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
|
---|
[20892] | 104 | print "size = [%dx%d]" % (s[0],s[1])
|
---|
[13339] | 105 | data=numpy.zeros((s[0],s[1]))
|
---|
| 106 | for i in xrange(s[0]):
|
---|
| 107 | for j in xrange(s[1]):
|
---|
| 108 | data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose
|
---|
[20892] | 109 | if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j])
|
---|
[13339] | 110 |
|
---|
| 111 | elif code == FormatToCode('MatArray'):
|
---|
[20097] | 112 | f.seek(reclen-4,1)
|
---|
[13339] | 113 | print "skipping %d bytes for code %d." % (code, reclen-4)
|
---|
| 114 |
|
---|
| 115 | elif code == FormatToCode('StringArray'):
|
---|
[20097] | 116 | f.seek(reclen-4,1)
|
---|
[13339] | 117 | print "skipping %d bytes for code %d." % (code, reclen-4)
|
---|
| 118 |
|
---|
| 119 | else:
|
---|
[20890] | 120 | raise TypeError('BinRead error message: data type: %d not supported yet! (%s)' % (code,recordname))
|
---|
[13339] | 121 |
|
---|
| 122 | f.close()
|
---|
[20097] | 123 | #}}}
|
---|
[13339] | 124 | def FormatToCode(format): # {{{
|
---|
| 125 | """
|
---|
| 126 | This routine takes the format string, and hardcodes it into an integer, which
|
---|
| 127 | is passed along the record, in order to identify the nature of the dataset being
|
---|
| 128 | sent.
|
---|
| 129 | """
|
---|
| 130 |
|
---|
[21758] | 131 | if format=='Boolean':
|
---|
[13339] | 132 | code=1
|
---|
[21758] | 133 | elif format=='Integer':
|
---|
[13339] | 134 | code=2
|
---|
[21758] | 135 | elif format=='Double':
|
---|
[13339] | 136 | code=3
|
---|
[21758] | 137 | elif format=='String':
|
---|
[13339] | 138 | code=4
|
---|
[21758] | 139 | elif format=='BooleanMat':
|
---|
[13339] | 140 | code=5
|
---|
[21758] | 141 | elif format=='IntMat':
|
---|
[13339] | 142 | code=6
|
---|
[21758] | 143 | elif format=='DoubleMat':
|
---|
[13339] | 144 | code=7
|
---|
[21758] | 145 | elif format=='MatArray':
|
---|
[13339] | 146 | code=8
|
---|
[21758] | 147 | elif format=='StringArray':
|
---|
[13339] | 148 | code=9
|
---|
| 149 | else:
|
---|
| 150 | raise InputError('FormatToCode error message: data type not supported yet!')
|
---|
| 151 |
|
---|
| 152 | return code
|
---|
| 153 | # }}}
|
---|
[20892] | 154 | def CodeToFormat(code): # {{{
|
---|
| 155 | """
|
---|
| 156 | This routine takes the format string, and hardcodes it into an integer, which
|
---|
| 157 | is passed along the record, in order to identify the nature of the dataset being
|
---|
| 158 | sent.
|
---|
| 159 | """
|
---|
[13339] | 160 |
|
---|
[20892] | 161 | if code==1:
|
---|
| 162 | format='Boolean'
|
---|
| 163 | elif code==2:
|
---|
| 164 | format='Integer'
|
---|
| 165 | elif code==3:
|
---|
| 166 | format='Double'
|
---|
| 167 | elif code==4:
|
---|
| 168 | format='String'
|
---|
| 169 | elif code==5:
|
---|
| 170 | format='BooleanMat'
|
---|
| 171 | elif code==6:
|
---|
| 172 | format='IntMat'
|
---|
| 173 | elif code==7:
|
---|
| 174 | format='DoubleMat'
|
---|
| 175 | elif code==8:
|
---|
| 176 | format='MatArray'
|
---|
| 177 | elif code==9:
|
---|
| 178 | format='StringArray'
|
---|
| 179 | else:
|
---|
| 180 | raise TypeError('FormatToCode error message: code %d not supported yet!' %code)
|
---|
| 181 |
|
---|
| 182 | return format
|
---|
| 183 | # }}}
|
---|
| 184 |
|
---|
[20097] | 185 | if __name__ == '__main__': #{{{
|
---|
[13339] | 186 | if 'PYTHONSTARTUP' in os.environ:
|
---|
| 187 | PYTHONSTARTUP=os.environ['PYTHONSTARTUP']
|
---|
| 188 | print 'PYTHONSTARTUP =',PYTHONSTARTUP
|
---|
| 189 | if os.path.exists(PYTHONSTARTUP):
|
---|
| 190 | try:
|
---|
| 191 | execfile(PYTHONSTARTUP)
|
---|
| 192 | except Exception as e:
|
---|
| 193 | print "PYTHONSTARTUP error: ",e
|
---|
| 194 | else:
|
---|
| 195 | print "PYTHONSTARTUP file '%s' does not exist." % PYTHONSTARTUP
|
---|
| 196 |
|
---|
| 197 | parser = argparse.ArgumentParser(description='BinRead - function to read binary input file.')
|
---|
| 198 | parser.add_argument('-f','--filin', help='name of binary input file', default='')
|
---|
[20097] | 199 | parser.add_argument('-o','--filout', help='optional name of text output file', default='')
|
---|
[20892] | 200 | parser.add_argument('-v','--verbose', help='optional level of output', default=0)
|
---|
[13339] | 201 | args = parser.parse_args()
|
---|
| 202 |
|
---|
[20892] | 203 | BinRead(args.filin, args.filout,args.verbose)
|
---|
[20219] | 204 | #}}}
|
---|