示例#1
0
def put(outlist, fname, writetype='w', oneperline=0, delimit=' '):
    """
Writes a passed mixed-type list (str and/or numbers) to an output
file, and then closes the file.  Default is overwrite the destination
file.

Usage:   put (outlist,fname,writetype='w',oneperline=0,delimit=' ')
Returns: None
"""
    if type(outlist) in [N.ArrayType]:
        aput(outlist, fname, writetype)
        return
    if type(outlist[0]) not in [ListType, TupleType]:  # 1D list
        outfile = open(fname, writetype)
        if not oneperline:
            outlist = pstat.list2string(outlist, delimit)
            outfile.write(outlist)
            outfile.write('\n')
        else:  # they want one element from the list on each file line
            for item in outlist:
                outfile.write(str(item) + '\n')
        outfile.close()
    else:  # 2D list (list-of-lists)
        outfile = open(fname, writetype)
        for row in outlist:
            outfile.write(pstat.list2string(row, delimit))
            outfile.write('\n')
        outfile.close()
    return None
示例#2
0
文件: io.py 项目: eddienko/SamPy
def put(outlist, fname, writetype="w", oneperline=0, delimit=" "):
    """
Writes a passed mixed-type list (str and/or numbers) to an output
file, and then closes the file.  Default is overwrite the destination
file.

Usage:   put (outlist,fname,writetype='w',oneperline=0,delimit=' ')
Returns: None
"""
    if type(outlist) in [N.ndarray]:
        aput(outlist, fname, writetype)
        return
    if type(outlist[0]) not in [ListType, TupleType]:  # 1D list
        outfile = open(fname, writetype)
        if not oneperline:
            outlist = pstat.list2string(outlist, delimit)
            outfile.write(outlist)
            outfile.write("\n")
        else:  # they want one element from the list on each file line
            for item in outlist:
                outfile.write(str(item) + "\n")
        outfile.close()
    else:  # 2D list (list-of-lists)
        outfile = open(fname, writetype)
        for row in outlist:
            outfile.write(pstat.list2string(row, delimit))
            outfile.write("\n")
        outfile.close()
    return None
示例#3
0
def mput(outarray, fname, writeheader=0, btype=N.int16):
    """
Save a file for use in matlab.
"""
    outarray = N.transpose(outarray)
    outdata = N.ravel(outarray).astype(btype)
    outdata = outdata.tostring()
    outfile = open(fname, 'wb')
    outfile.write(outdata)
    outfile.close()
    if writeheader == 1:
        try:
            suffixindex = string.rfind(fname, '.')
            hdrname = fname[0:suffixindex]
        except ValueError:
            hdrname = fname
        if len(outarray.shape) == 2:
            hdr = [outarray.shape[1], outarray.shape[0], 1, 0]
        else:
            hdr = [
                outarray.shape[2], outarray.shape[1], outarray.shape[0], 0,
                '\n'
            ]
        print hdrname + '.hdr'
        outfile = open(hdrname + '.hdr', 'w')
        outfile.write(pstat.list2string(hdr))
        outfile.close()
    return None
示例#4
0
文件: io.py 项目: eddienko/SamPy
def mput(outarray, fname, writeheader=0, btype=N.int16):
    """
Save a file for use in matlab.
"""
    outarray = N.transpose(outarray)
    outdata = N.ravel(outarray).astype(btype)
    outdata = outdata.tostring()
    outfile = open(fname, "wb")
    outfile.write(outdata)
    outfile.close()
    if writeheader == 1:
        try:
            suffixindex = string.rfind(fname, ".")
            hdrname = fname[0:suffixindex]
        except ValueError:
            hdrname = fname
        if len(outarray.shape) == 2:
            hdr = [outarray.shape[1], outarray.shape[0], 1, 0]
        else:
            hdr = [outarray.shape[2], outarray.shape[1], outarray.shape[0], 0, "\n"]
        print hdrname + ".hdr"
        outfile = open(hdrname + ".hdr", "w")
        outfile.write(pstat.list2string(hdr))
        outfile.close()
    return None
示例#5
0
def binput(outarray, fname, packtype=None, writetype='wb'):
    """
Unravels outarray and writes the data to a file, always in LittleEndian
format, along with a header file containing the original data shape. Default
is overwrite the destination file. Tries to figure out packtype from
4th-to-last character in filename. Thus, the routine understands these
file formats ...

1bin=Int8, sbin=int16, ibin=Int32, fbin=Float32, dbin=Float64, etc.

Usage:  binput(outarray,filename,packtype=None,writetype='wb')
"""
    if not packtype:
        packtype = fname[-4]

    # a speck of error checking
    if packtype == N.int16 and outarray.typecode() == 'f':
        # check to see if there's data loss
        if max(N.ravel(outarray)) > 32767 or min(N.ravel(outarray)) < -32768:
            print "*** WARNING: CONVERTING FLOAT DATA TO OUT-OF RANGE int16 DATA"
    outdata = N.ravel(outarray).astype(packtype)

    # force the data on disk to be LittleEndian (for more efficient PC/Linux use)
    if not N.LittleEndian:
        outdata = outdata.byteswapped()
    outdata = outdata.tostring()
    outfile = open(fname, writetype)
    outfile.write(outdata)
    outfile.close()

    # Now, write the header file
    try:
        suffixindex = string.rfind(fname, '.')
        hdrname = fname[0:suffixindex +
                        2] + 'hdr'  # include .s or .f or .1 or whatever
    except ValueError:
        hdrname = fname
    hdr = outarray.shape
    print hdrname
    outfile = open(hdrname, 'w')
    outfile.write(pstat.list2string(hdr))
    outfile.close()
    return None
示例#6
0
文件: io.py 项目: eddienko/SamPy
def binput(outarray, fname, packtype=None, writetype="wb"):
    """
Unravels outarray and writes the data to a file, always in LittleEndian
format, along with a header file containing the original data shape. Default
is overwrite the destination file. Tries to figure out packtype from
4th-to-last character in filename. Thus, the routine understands these
file formats ...

1bin=int8, sbin=int16, ibin=int32, fbin=float32, dbin=float64, etc.

Usage:  binput(outarray,filename,packtype=None,writetype='wb')
"""
    if not packtype:
        packtype = fname[-4]

    # a speck of error checking
    if packtype == N.int16 and outarray.dtype.char == "f":
        # check to see if there's data loss
        if max(N.ravel(outarray)) > 32767 or min(N.ravel(outarray)) < -32768:
            print "*** WARNING: CONVERTING FLOAT DATA TO OUT-OF RANGE INT16 DATA"
    outdata = N.ravel(outarray).astype(packtype)

    # force the data on disk to be little_endian (for more efficient PC/Linux use)
    if not N.little_endian:
        outdata = outdata.byteswap()
    outdata = outdata.tostring()
    outfile = open(fname, writetype)
    outfile.write(outdata)
    outfile.close()

    # Now, write the header file
    try:
        suffixindex = string.rfind(fname, ".")
        hdrname = fname[0 : suffixindex + 2] + "hdr"  # include .s or .f or .1 or whatever
    except ValueError:
        hdrname = fname
    hdr = outarray.shape
    print hdrname
    outfile = open(hdrname, "w")
    outfile.write(pstat.list2string(hdr))
    outfile.close()
    return None
示例#7
0
def bput(outarray, fname, writeheader=0, packtype=N.int16, writetype='wb'):
    """
Writes the passed array to a binary output file, and then closes
the file.  Default is overwrite the destination file.

Usage:   bput (outarray,filename,writeheader=0,packtype=N.int16,writetype='wb')
"""
    suffix = fname[-6:]
    if suffix == 'bshort':
        packtype = N.int16
    elif suffix == 'bfloat':
        packtype = N.Float32
    else:
        print 'Not a bshort or bfloat file.  Using packtype=', packtype

    outdata = N.ravel(outarray).astype(packtype)
    littleEndian = (struct.pack('i', 1) == struct.pack('<i', 1))
    if littleEndian and os.uname()[0] <> 'Linux':
        outdata = outdata.byteswapped()
    outdata = outdata.tostring()
    outfile = open(fname, writetype)
    outfile.write(outdata)
    outfile.close()
    if writeheader == 1:
        try:
            suffixindex = string.rfind(fname, '.')
            hdrname = fname[0:suffixindex]
        except ValueError:
            hdrname = fname
        if len(outarray.shape) == 2:
            hdr = [outarray.shape[0], outarray.shape[1], 1, 0]
        else:
            hdr = [
                outarray.shape[1], outarray.shape[2], outarray.shape[0], 0,
                '\n'
            ]
        print hdrname + '.hdr'
        outfile = open(hdrname + '.hdr', 'w')
        outfile.write(pstat.list2string(hdr))
        outfile.close()
    return None
示例#8
0
文件: io.py 项目: eddienko/SamPy
def bput(outarray, fname, writeheader=0, packtype=N.int16, writetype="wb"):
    """
Writes the passed array to a binary output file, and then closes
the file.  Default is overwrite the destination file.

Usage:   bput (outarray,filename,writeheader=0,packtype=N.int16,writetype='wb')
"""
    suffix = fname[-6:]
    if suffix == "bshort":
        packtype = N.int16
    elif suffix == "bfloat":
        packtype = N.float32
    else:
        print "Not a bshort or bfloat file.  Using packtype=", packtype

    outdata = N.ravel(outarray).astype(packtype)
    #    littleEndian = ( struct.pack('i',1)==struct.pack('<i',1) )
    #    if littleEndian:
    #        outdata = outdata.byteswap()
    outdata = outdata.tostring()
    outfile = open(fname, writetype)
    outfile.write(outdata)
    outfile.close()
    if writeheader == 1:
        try:
            suffixindex = string.rfind(fname, ".")
            hdrname = fname[0:suffixindex]
        except ValueError:
            hdrname = fname
        if len(outarray.shape) == 2:
            hdr = [outarray.shape[0], outarray.shape[1], 1, 0]
        else:
            hdr = [outarray.shape[1], outarray.shape[2], outarray.shape[0], 0, "\n"]
        print hdrname + ".hdr"
        outfile = open(hdrname + ".hdr", "w")
        outfile.write(pstat.list2string(hdr))
        outfile.close()
    return None
示例#9
0
文件: io.py 项目: gandalfar/exam
def bput(outarray,fname,writeheader=0,packtype=N.Int16,writetype='wb'):
    """
Writes the passed array to a binary output file, and then closes
the file.  Default is overwrite the destination file.

Usage:   bput (outarray,filename,writeheader=0,packtype=N.Int16,writetype='wb')
"""
    suffix = fname[-6:]
    if suffix == 'bshort':
        packtype = N.Int16
    elif suffix == 'bfloat':
        packtype = N.Float32
    else:
        print 'Not a bshort or bfloat file.  Using packtype=',packtype

    outdata = N.ravel(outarray).astype(packtype)
    littleEndian = ( struct.pack('i',1)==struct.pack('<i',1) )
    if littleEndian and os.uname()[0]<>'Linux':
        outdata = outdata.byteswapped()
    outdata = outdata.tostring()
    outfile = open(fname,writetype)
    outfile.write(outdata)
    outfile.close()
    if writeheader == 1:
        try:
            suffixindex = string.rfind(fname,'.')
            hdrname = fname[0:suffixindex]
        except ValueError:
            hdrname = fname
        if len(outarray.shape) == 2:
            hdr = [outarray.shape[0],outarray.shape[1], 1, 0]
        else:
            hdr = [outarray.shape[1],outarray.shape[2],outarray.shape[0], 0,'\n']
        print hdrname+'.hdr'
        outfile = open(hdrname+'.hdr','w')
        outfile.write(pstat.list2string(hdr))
        outfile.close()
    return None