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
评论列表
文章目录