Changeset 20892
- Timestamp:
- 07/13/16 10:55:07 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/scripts/BinRead.py
r20890 r20892 8 8 import argparse 9 9 10 def BinRead(filin,filout='' ): #{{{10 def BinRead(filin,filout='',verbose=0): #{{{ 11 11 12 12 print "reading binary file." … … 23 23 print "probable EOF: %s" % e 24 24 break 25 print "\nrecordnamesize = \"%d\"" % (recordnamesize) 25 26 print "============================================================================" 27 if verbose>2: 28 print "\nrecordnamesize = \"%d\"" % (recordnamesize) 26 29 recordname=struct.unpack('%ds' % recordnamesize,f.read(recordnamesize))[0] 27 print " recordname = '%s'" % recordname30 print "field: %s" % recordname 28 31 29 32 #Step 2: read the data itself. 30 33 #first read length of record 31 34 reclen=struct.unpack('i',f.read(struct.calcsize('i')))[0] 32 print "reclen = %d" % reclen 35 if verbose>1: 36 print "reclen = %d" % reclen 33 37 34 38 #read data code: 35 39 code=struct.unpack('i',f.read(struct.calcsize('i')))[0] 36 print "code = %d" % code 40 #print "code = %d (%s)" % (code,CodeToFormat(code)) 41 print "Format = %s" % CodeToFormat(code) 37 42 38 43 if code == FormatToCode('Boolean'): 39 44 # bval=struct.unpack('b',f.read(reclen-struct.calcsize('i')))[0] 40 45 bval=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0] 41 print " bval= %d" % bval46 print "value = %d" % bval 42 47 43 48 elif code == FormatToCode('Integer'): 44 49 ival=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0] 45 print " ival= %d" % ival50 print "value = %d" % ival 46 51 47 52 elif code == FormatToCode('Double'): 48 53 dval=struct.unpack('d',f.read(reclen-struct.calcsize('i')))[0] 49 print " dval= %f" % dval54 print "value = %f" % dval 50 55 51 56 elif code == FormatToCode('String'): 52 57 strlen=struct.unpack('i',f.read(struct.calcsize('i')))[0] 53 print "strlen = %d" % strlen 58 if verbose>1: 59 print "strlen = %d" % strlen 54 60 sval=struct.unpack('%ds' % strlen,f.read(strlen))[0] 55 print " sval= '%s'" % sval61 print "value = '%s'" % sval 56 62 57 63 elif code == FormatToCode('BooleanMat'): … … 64 70 s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 65 71 s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 66 print "s = [%dx%d]" % (s[0],s[1])72 print "size = [%dx%d]" % (s[0],s[1]) 67 73 data=numpy.zeros((s[0],s[1])) 68 74 for i in xrange(s[0]): 69 75 for j in xrange(s[1]): 70 76 data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose 71 print "data[%d,%d] = %f" % (i,j,data[i][j])77 if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j]) 72 78 73 79 elif code == FormatToCode('IntMat'): … … 80 86 s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 81 87 s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 82 print "s = [%dx%d]" % (s[0],s[1])88 print "size = [%dx%d]" % (s[0],s[1]) 83 89 data=numpy.zeros((s[0],s[1])) 84 90 for i in xrange(s[0]): 85 91 for j in xrange(s[1]): 86 92 data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose 87 print "data[%d,%d] = %f" % (i,j,data[i][j])93 if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j]) 88 94 89 95 elif code == FormatToCode('DoubleMat'): … … 96 102 s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 97 103 s[1]=struct.unpack('i',f.read(struct.calcsize('i')))[0] 98 print "s = [%dx%d]" % (s[0],s[1])104 print "size = [%dx%d]" % (s[0],s[1]) 99 105 data=numpy.zeros((s[0],s[1])) 100 106 for i in xrange(s[0]): 101 107 for j in xrange(s[1]): 102 108 data[i][j]=struct.unpack('d',f.read(struct.calcsize('d')))[0] #get to the "c" convention, hence the transpose 103 print "data[%d,%d] = %f" % (i,j,data[i][j])109 if verbose>2: print "data[%d,%d] = %f" % (i,j,data[i][j]) 104 110 105 111 elif code == FormatToCode('MatArray'): … … 146 152 return code 147 153 # }}} 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 """ 160 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 # }}} 148 184 149 185 if __name__ == '__main__': #{{{ … … 162 198 parser.add_argument('-f','--filin', help='name of binary input file', default='') 163 199 parser.add_argument('-o','--filout', help='optional name of text output file', default='') 200 parser.add_argument('-v','--verbose', help='optional level of output', default=0) 164 201 args = parser.parse_args() 165 202 166 203 from MatlabFuncs import * 167 204 168 BinRead(args.filin, args.filout )205 BinRead(args.filin, args.filout,args.verbose) 169 206 #}}}
Note:
See TracChangeset
for help on using the changeset viewer.