Ignore:
Timestamp:
10/11/19 00:24:15 (5 years ago)
Author:
bdef
Message:

CHG: syntax cahnge to meet most of Pep8 requirement

File:
1 edited

Legend:

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

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