Changeset 20892


Ignore:
Timestamp:
07/13/16 10:55:07 (9 years ago)
Author:
Mathieu Morlighem
Message:

CHG: more refined BinRead

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/scripts/BinRead.py

    r20890 r20892  
    88import argparse
    99
    10 def BinRead(filin,filout=''): #{{{
     10def BinRead(filin,filout='',verbose=0): #{{{
    1111
    1212        print "reading binary file."
     
    2323                        print "probable EOF: %s" % e
    2424                        break
    25                 print "\nrecordnamesize = \"%d\"" % (recordnamesize)
     25
     26                print "============================================================================"
     27                if verbose>2:
     28                        print "\nrecordnamesize = \"%d\"" % (recordnamesize)
    2629                recordname=struct.unpack('%ds' % recordnamesize,f.read(recordnamesize))[0]
    27                 print "recordname = '%s'" % recordname
     30                print "field: %s" % recordname
    2831
    2932                #Step 2: read the data itself.
    3033                #first read length of record
    3134                reclen=struct.unpack('i',f.read(struct.calcsize('i')))[0]
    32                 print "reclen = %d" % reclen
     35                if verbose>1:
     36                        print "reclen = %d" % reclen
    3337
    3438                #read data code:
    3539                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)
    3742
    3843                if   code == FormatToCode('Boolean'):
    3944#                       bval=struct.unpack('b',f.read(reclen-struct.calcsize('i')))[0]
    4045                        bval=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0]
    41                         print "bval = %d" % bval
     46                        print "value = %d" % bval
    4247
    4348                elif code == FormatToCode('Integer'):
    4449                        ival=struct.unpack('i',f.read(reclen-struct.calcsize('i')))[0]
    45                         print "ival = %d" % ival
     50                        print "value = %d" % ival
    4651
    4752                elif code == FormatToCode('Double'):
    4853                        dval=struct.unpack('d',f.read(reclen-struct.calcsize('i')))[0]
    49                         print "dval = %f" % dval
     54                        print "value = %f" % dval
    5055
    5156                elif code == FormatToCode('String'):
    5257                        strlen=struct.unpack('i',f.read(struct.calcsize('i')))[0]
    53                         print "strlen = %d" % strlen
     58                        if verbose>1:
     59                                print "strlen = %d" % strlen
    5460                        sval=struct.unpack('%ds' % strlen,f.read(strlen))[0]
    55                         print "sval = '%s'" % sval
     61                        print "value = '%s'" % sval
    5662
    5763                elif code == FormatToCode('BooleanMat'):
     
    6470                        s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
    6571                        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])
    6773                        data=numpy.zeros((s[0],s[1]))
    6874                        for i in xrange(s[0]):
    6975                                for j in xrange(s[1]):
    7076                                        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])
    7278
    7379                elif code == FormatToCode('IntMat'):
     
    8086                        s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
    8187                        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])
    8389                        data=numpy.zeros((s[0],s[1]))
    8490                        for i in xrange(s[0]):
    8591                                for j in xrange(s[1]):
    8692                                        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])
    8894
    8995                elif code == FormatToCode('DoubleMat'):
     
    96102                        s[0]=struct.unpack('i',f.read(struct.calcsize('i')))[0]
    97103                        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])
    99105                        data=numpy.zeros((s[0],s[1]))
    100106                        for i in xrange(s[0]):
    101107                                for j in xrange(s[1]):
    102108                                        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])
    104110
    105111                elif code == FormatToCode('MatArray'):
     
    146152        return code
    147153# }}}
     154def 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# }}}
    148184
    149185if __name__ == '__main__': #{{{
     
    162198        parser.add_argument('-f','--filin', help='name of binary input file', default='')
    163199        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)
    164201        args = parser.parse_args()
    165202
    166203        from MatlabFuncs import *
    167204
    168         BinRead(args.filin, args.filout)
     205        BinRead(args.filin, args.filout,args.verbose)
    169206#}}}
Note: See TracChangeset for help on using the changeset viewer.