Exemplo n.º 1
0
    def fort_write(self,fmt,*args):
        """Write a Fortran binary record.

        Inputs:

          fmt -- If a string then it represents the same format string as
                 used by struct.pack.  The remaining arguments are passed
                 to struct.pack.

                 If fmt is an array, then this array will be written as
                 a Fortran record using the output type args[0].

          *args -- Arguments representing data to write.
        """
        if self.format == 'ieee-le':
            nfmt = "<i"
        elif self.format == 'ieee-be':
            nfmt = ">i"
        else:
            nfmt = "i"
        if isinstance(fmt, basestring):
            if self.format == 'ieee-le':
                fmt = "<"+fmt
            elif self.format == 'ieee-be':
                fmt = ">"+fmt
            str = apply(struct.pack,(fmt,)+args)
            strlen = struct.pack(nfmt,len(str))
            self.write(strlen)
            self.write(str)
            self.write(strlen)
        elif type(fmt) == type(array([0])):
            if len(args) > 0:
                sz,mtype = getsize_type(args[0])
            else:
                sz,mtype = getsize_type(fmt.dtype.char)
            count = product(fmt.shape,axis=0)
            strlen = struct.pack(nfmt,count*sz)
            self.write(strlen)
            numpyio.fwrite(self.file,count,fmt,mtype,self.bs)
            self.write(strlen)
        else:
            raise TypeError, "Unknown type in first argument"
Exemplo n.º 2
0
    def fort_write(self, fmt, *args):
        """Write a Fortran binary record.

        Inputs:

          fmt -- If a string then it represents the same format string as
                 used by struct.pack.  The remaining arguments are passed
                 to struct.pack.

                 If fmt is an array, then this array will be written as
                 a Fortran record using the output type args[0].

          *args -- Arguments representing data to write.
        """
        if self.format == 'ieee-le':
            nfmt = "<i"
        elif self.format == 'ieee-be':
            nfmt = ">i"
        else:
            nfmt = "i"
        if isinstance(fmt, basestring):
            if self.format == 'ieee-le':
                fmt = "<" + fmt
            elif self.format == 'ieee-be':
                fmt = ">" + fmt
            str = apply(struct.pack, (fmt, ) + args)
            strlen = struct.pack(nfmt, len(str))
            self.write(strlen)
            self.write(str)
            self.write(strlen)
        elif type(fmt) == type(array([0])):
            if len(args) > 0:
                sz, mtype = getsize_type(args[0])
            else:
                sz, mtype = getsize_type(fmt.dtype.char)
            count = product(fmt.shape, axis=0)
            strlen = struct.pack(nfmt, count * sz)
            self.write(strlen)
            numpyio.fwrite(self.file, count, fmt, mtype, self.bs)
            self.write(strlen)
        else:
            raise TypeError, "Unknown type in first argument"
Exemplo n.º 3
0
    def write(self,data,mtype=None,bs=None):
        """Write to open file object the flattened numpy array data.

        Inputs:

          data -- the numpy array to write.
          mtype -- a string indicating the binary type to write.
                   The default is the type of data. If necessary a cast is made.
                   unsigned byte  : 'B', 'uchar', 'byte' 'unsigned char', 'int8',
                                    'integer*1'
                   character      : 'S1', 'char', 'char*1'
                   signed char    : 'b', 'schar', 'signed char'
                   short          : 'h', 'short', 'int16', 'integer*2'
                   unsigned short : 'H', 'ushort','uint16','unsigned short'
                   int            : 'i', 'int'
                   unsigned int   : 'I', 'uint32','uint','unsigned int'
                   int32           : 'u4', 'int32', 'integer*4'
                   float          : 'f', 'float', 'float32', 'real*4'
                   double         : 'd', 'double', 'float64', 'real*8'
                   complex float  : 'F', 'complex float', 'complex*8', 'complex64'
                   complex double : 'D', 'complex', 'complex double', 'complex*16',
                                    'complex128'
        """
        if bs is None:
            bs = self.bs
        else:
            bs = (bs == 1)
        if isinstance(data, str):
            N, buf = len(data), buffer(data)
            data = ndarray(shape=(N,),dtype='B',buffer=buf)
        else:
            data = asarray(data)
        if mtype is None:
            mtype = data.dtype.char
        howmany,mtype = getsize_type(mtype)
        count = product(data.shape,axis=0)
        numpyio.fwrite(self.file,count,data,mtype,bs)
        return
Exemplo n.º 4
0
    def write(self, data, mtype=None, bs=None):
        """Write to open file object the flattened numpy array data.

        Inputs:

          data -- the numpy array to write.
          mtype -- a string indicating the binary type to write.
                   The default is the type of data. If necessary a cast is made.
                   unsigned byte  : 'B', 'uchar', 'byte' 'unsigned char', 'int8',
                                    'integer*1'
                   character      : 'S1', 'char', 'char*1'
                   signed char    : 'b', 'schar', 'signed char'
                   short          : 'h', 'short', 'int16', 'integer*2'
                   unsigned short : 'H', 'ushort','uint16','unsigned short'
                   int            : 'i', 'int'
                   unsigned int   : 'I', 'uint32','uint','unsigned int'
                   int32           : 'u4', 'int32', 'integer*4'
                   float          : 'f', 'float', 'float32', 'real*4'
                   double         : 'd', 'double', 'float64', 'real*8'
                   complex float  : 'F', 'complex float', 'complex*8', 'complex64'
                   complex double : 'D', 'complex', 'complex double', 'complex*16',
                                    'complex128'
        """
        if bs is None:
            bs = self.bs
        else:
            bs = (bs == 1)
        if isinstance(data, str):
            N, buf = len(data), buffer(data)
            data = ndarray(shape=(N, ), dtype='B', buffer=buf)
        else:
            data = asarray(data)
        if mtype is None:
            mtype = data.dtype.char
        howmany, mtype = getsize_type(mtype)
        count = product(data.shape, axis=0)
        numpyio.fwrite(self.file, count, data, mtype, bs)
        return
Exemplo n.º 5
0
from sys import argv

print "\nReading info from %s.hdr and" % argv[1]
print "%s.dat\n" % argv[1]
print "Writing %s.raw\n" % argv[1]

HEADERLEN = 640
BLOCKLEN = 49152

# Read the header file

file = open(argv[1]+'.hdr', 'r')
data = fread(file, HEADERLEN+8, 'b')
file.close()
header = data[4:-4]
infile = open(argv[1]+'.dat', 'r')
outfile = open(argv[1]+'.raw', 'w')

# Read and write the raw data

while (1):
    data = fread(infile, BLOCKLEN+8, 'b')
    if (len(data)==BLOCKLEN+8):
        fwrite(outfile, HEADERLEN, header, 'b')
        fwrite(outfile, BLOCKLEN, data[4:-4], 'b')
    else:
        break
print ''
infile.close()
outfile.close()
Exemplo n.º 6
0
from sys import argv

print("\nReading info from %s.hdr and" % argv[1])
print("%s.dat\n" % argv[1])
print("Writing %s.raw\n" % argv[1])

HEADERLEN = 640
BLOCKLEN = 49152

# Read the header file

file = open(argv[1] + '.hdr', 'r')
data = fread(file, HEADERLEN + 8, 'b')
file.close()
header = data[4:-4]
infile = open(argv[1] + '.dat', 'r')
outfile = open(argv[1] + '.raw', 'w')

# Read and write the raw data

while (1):
    data = fread(infile, BLOCKLEN + 8, 'b')
    if (len(data) == BLOCKLEN + 8):
        fwrite(outfile, HEADERLEN, header, 'b')
        fwrite(outfile, BLOCKLEN, data[4:-4], 'b')
    else:
        break
print('')
infile.close()
outfile.close()