예제 #1
0
파일: mIO.py 프로젝트: lbaehren/lofarsoft
 def fort_write(self,fmt,*args):
     if self.format == 'ieee-le':
         nfmt = "<i"
     elif self.format == 'ieee-be':
         nfmt = ">i"
     else:
         nfmt = "i"
     if type(fmt) == type(''):
         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.fid.write(strlen)
         self.fid.write(str)
         self.fid.write(strlen)
     elif type(fmt) == type(array([0])):
         sz,mtype = getsize_type(args[0])
         count = product(fmt.shape)
         strlen = struct.pack(nfmt,count*sz)
         self.fid.write(strlen)
         numpyio.fwrite(self.fid,count,fmt,mtype,self.bs)
         self.fid.write(strlen)
     else:
         raise TypeError, "Unknown type in first argument"
예제 #2
0
 def save(self, filename):
     print "Saving %s"%filename
     f = open(filename,"wb")
     print >>f, "%d %d %d"%(self.x_res, self.y_res, self.z_res)
     print >>f, "%g %g %g"%(self.x_min, self.y_min, self.z_min)
     print >>f, "%g %g %g"%(self.x_max, self.y_max, self.z_max)
     fwrite(f, self.grid.size, self.grid)
     f.close()
예제 #3
0
	def savefile( self ):
		fd = open('./dat/050-A_binary.dat', 'wb')
		fwrite( fd, self.A.size, self.A )
		fd.close()

		fd = open('./dat/050-dA_binary.dat', 'wb')
		fwrite( fd, self.dA.size, self.dA )
		fd.close()
예제 #4
0
    def write(self,fn=None,x=None,fs=None,channels=None,format=None,bitrate=None,audiolab=False):
        """Writes data x (as numpy array of floats scaled between -1 and 1) at sampling rate fs to file fn
             fn: name of file to create (defualt is self.fn)
              x: data buffer (default is self.x)
             fs: sampling rate (default is self.fs)
       channels: number of channels (default is x.shape[1])
         format: output file format as used by -f flag of ffmpeg (will attempt to infer from extension of fn if omitted)
        bitrate: bitrate for compressed codec. For mp3 and mp4, 196k is default.
       audiolab: if True, uses scikits.audiolab in place of ffmpeg.  In this case, the output will be a wav file 
        Warning: overwrites fn if fn exists
        """
        from scipy.io.numpyio import fwrite, fread
#        import copy
        import tempfile

        if fn is None :
            fn=self.fn
        if x is None:
            x=self.x
        if fs is None:
            fs=self.fs

        if channels is None:
            if len(x.shape)==2 :
                channels=x.shape[1]
            else:
                channels=1

        if audiolab:
            import scikits.audiolab 
            scikits.audiolab.wavwrite(x,fn,fs)
            return

        [ignore,suffix] = os.path.splitext(fn)
        [fdir,file]=os.path.split(os.path.abspath(fn))
        tf=tempfile.NamedTemporaryFile(dir=fdir)     
        xout=(x.flatten()*32768).astype('int16')
        fwrite(tf.file,len(xout),xout)

                
        #set up format for known file types
        if format is None:
            format=suffix[1:]
        format_text='-f ' + format
        bitrate_text=''
        if bitrate is not None :
            bitrate_text ='-ab %i' % bitrate
        else :
            if format=='mp4' or format=='mp3' :
                bitrate_text='-ab 196k'
                
        #print 'Saving x of shape=%s fs=%i format=%s to file %s' % (str(x.shape),fs,format,fn)
        #-y is not well documented but forces overwrite of target file
        if os.path.exists(fn) :
            os.unlink(fn)
        cmd='ffmpeg -ar %i -f s16le -ac %i -i %s %s %s %s' % (fs,channels,tf.name,bitrate_text,format_text,fn)
        os.system(cmd)
        tf.close()
예제 #5
0
 def AppendData(self, data):
     self.datarows += len(data)
     for filename in self.filenames:
         f = open(filename, 'r+b')
         f.seek(self.datarowsptr, 0)
         f.write(pack('=l', self.datarows))
         f.seek(0, 2)  #move pointer to end of file
         fwrite(f, data.size, data)  #write a second's worth of data to disk
         f.close()
예제 #6
0
	def savefile( self, path ):
		path1 = path + 'A_binary.dat'
		path2 = path + 'dA_binary.dat'

		fd = open( path1, 'wb' )
		fwrite( fd, self.A.size, self.A )
		fd.close()

		fd = open( path2, 'wb' )
		fwrite( fd, self.dA.size, self.dA )
		fd.close()
예제 #7
0
def write(dataset, filename):
    if not isinstance(dataset, numpy.ndarray):
        raise FLANNException("Can only save numpy arrays")

    with open(filename, 'w') as fd_meta:
        fd_meta.write(\
"""BINARY
%s
%d
%d
%s"""%(filename+".bin",dataset.shape[1],dataset.shape[0],dataset.dtype.name))
    with open(filename + ".bin", 'wb') as fd:
        fwrite(fd, dataset.size, dataset)
예제 #8
0
def write(dataset, filename):
    if not isinstance(dataset,numpy.ndarray):
        raise FLANNException("Can only save numpy arrays")
    
    with open(filename, 'w') as fd_meta:
        fd_meta.write(\
"""BINARY
%s
%d
%d
%s"""%(filename+".bin",dataset.shape[1],dataset.shape[0],dataset.dtype.name))
    with open(filename+".bin", 'wb') as fd:
        fwrite(fd, dataset.size, dataset)
예제 #9
0
	def savefile( self, path ):
		cuda.memcpy_dtoh( self.dA, self.dev_dA )

		path1 = path + '-A_binary.dat'
		path2 = path + '-dA_binary.dat'

		fd = open( path1, 'wb' )
		fwrite( fd, self.A.size, self.A )
		fd.close()

		fd = open( path2, 'wb' )
		fwrite( fd, self.dA.size, self.dA )
		fd.close()
예제 #10
0
 def test_basic(self):
     # Generate some data
     a = 255*rand(20)
     # Open a file
     fname = tempfile.mktemp('.dat')
     fid = open(fname,"wb")
     # Write the data as shorts
     numpyio.fwrite(fid,20,a,N.Int16)
     fid.close()
     # Reopen the file and read in data
     fid = open(fname,"rb")
     if verbose >= 3:
         print "\nDon't worry about a warning regarding the number of bytes read."
     b = numpyio.fread(fid,1000000,N.Int16,N.Int)
     fid.close()
     assert(N.product(a.astype(N.Int16) == b,axis=0))
     os.remove(fname)
예제 #11
0
 def test_basic(self):
     # Generate some data
     a = 255 * rand(20)
     # Open a file
     fname = tempfile.mktemp('.dat')
     fid = open(fname, "wb")
     # Write the data as shorts
     numpyio.fwrite(fid, 20, a, N.Int16)
     fid.close()
     # Reopen the file and read in data
     fid = open(fname, "rb")
     if verbose >= 3:
         print "\nDon't worry about a warning regarding the number of bytes read."
     b = numpyio.fread(fid, 1000000, N.Int16, N.Int)
     fid.close()
     assert (N.product(a.astype(N.Int16) == b, axis=0))
     os.remove(fname)
예제 #12
0
	def save_geometry_array(s, save_filename):
		print 'Save the geo-file as \'%s\'...' % save_filename
		save_filename_line	=	save_filename.replace('.geo','.line.geo')
		save_filename_area	=	save_filename.replace('.geo','.area.geo')

		from scipy import array
		data_line = (s.line_x, s.line_y, s.line_z)
		data_area = (s.area_x, s.area_y, s.area_z)
		data_line_array = array(data_line)
		data_area_array = array(data_area)

		from scipy.io.numpyio import fwrite
		fd	=	open(save_filename_line,'wb')
		fwrite(fd, data_line_array.size, data_line_array)
		fd.close()
		fd	=	open(save_filename_area,'wb')
		fwrite(fd, data_area_array.size, data_area_array)
		fd.close()
예제 #13
0
def write_structure_files(structures):
    print 'Writing the structure files...'
    for s_number, structure in enumerate(structures):
        print '%d) %s' %(s_number, structure.__name__)

        info_str = '#!/usr/bin/env python\n \
                __name__ = %s\n \
                matter = %s\n \
                DER = discretized_effective_region = %s\n \
                DERS = discretized_effective_sides = %s\n \
                DERC = discretized_effective_center = %s'
                % ( structure.__name__, \
                repr(structure.matter), \
                repr(structure.discretized_effective_region), \
                repr(structure.discretized_effective_sides), \
                repr(structure.discretized_effective_center) )
        filename = './structures/%.3d_%s_info.py' \
                % (s_number, structure.__name__)
        print filename
        info_file = open(filename, 'w')
        info_file.write(info_str)
        info_file.close()

        print 'Calculate the arrays of the intersection points...'
        structure.make_intersection_points_arrays()
        ISPTs_array_x = structure.intersection_points_arrays[0]
        ISPTs_array_y = structure.intersection_points_arrays[1]
        ISPTs_array_z = structure.intersection_points_arrays[2]

        filename = './structures/%.3d_%s_ispts_x.scbin' \
                % (s_number, structure.__name__)
        print filename
        array_file = open(filename, 'wb')
        fwrite(array_file, ISPTs_array_x.size, ISPTs_array_x)
        array_file.close()
        filename = './structures/%.3d_%s_ispts_y.scbin' \
                % (s_number, structure.__name__)
        print filename
        array_file = open(filename, 'wb')
        fwrite(array_file, ISPTs_array_y.size, ISPTs_array_y)
        array_file.close()
        filename = './structures/%.3d_%s_ispts_z.scbin' \
                % (s_number, structure.__name__)
        print filename
        array_file = open(filename, 'wb')
        fwrite(array_file, ISPTs_array_z.size, ISPTs_array_z)
        array_file.close()
예제 #14
0
def petsc_binary_write(A, filename):

    fd = open(filename, "wb")

    if np.rank(A) == 1:  # is vector
        m = np.shape(A)[0]
        n = 1
    elif np.rank(A) == 2:  # is matrix
        m = np.shape(A)[0]
        n = np.shape(A)[1]

    if sparse.issparse(A):
        majic = 1.2345678910e-30
        diag = sparse.extract_diagonal(A)
        for i in xrange(len(diag)):
            if diag[i] == 0:
                diag[i] = majic

        nz = sparse.spmatrix.getnnz(A)
        harr = np.array([1211216, m, n, nz])
        fwrite(fd, np.size(harr), harr, "i")

        # nz_per_row =

        # write nz_per_row

    #    fwrite(fd,n_nz,'int32');  %nonzeros per row
    #    [i,j,s] = find(A');
    #    fwrite(fd,i-1,'int32');
    #    for i=1:nz
    #      if s(i) == majic
    #        s(i) = 0;
    #      end
    #    end
    #    fwrite(fd,s,'double');

    else:
        size = m * n
        harr = np.array([code_vector, size])  # header
        fwrite(fd, 2, harr, "i", 1)
        fwrite(fd, size, A, "d", 1)

    pdb.set_trace()

    fd.close()
예제 #15
0
# array shape or datatype, but is quick and easy.
# 
# SciPy provides fwrite() from scipy.io.numpyio. You have to set the size
# of your data, and optionally, its type (integer, short, float, etc; see
# [1](http://docs.neuroinf.de/api/scipy/scipy.io.numpyio-module.html)).
# 
# For reading binary files, scipy.io.numpyio provides fread(). You have to
# know the datatype of your array, its size and its shape.
# 
# <codecell>


>>> from scipy.io.numpyio import fwrite, fread
>>> data = zeros((3,3))
>>>#write:  fd = open('myfile.dat', 'wb')
>>> fwrite(fd, data.size, data)
>>> fd.close()
>>>#read:
>>> fd = open('myfile.dat', 'rb')
>>> datatype = 'i'
>>> size = 9
>>> shape = (3,3)
>>> read_data = fread(fd, size, datatype)
>>> read_data = data.reshape(shape)

# <markdowncell>

# Or, you can simply use and . Following the previous example:
# 
# <codecell>
예제 #16
0
파일: mIO.py 프로젝트: lbaehren/lofarsoft
 def fwrite(self,data,mtype):
     howmany,mtype = getsize_type(mtype)
     data = asarray(data)
     count = product(data.shape)
     val = numpyio.fwrite(self.fid,count,data,mtype,self.bs)
     return val
예제 #17
0
from scipy.io.numpyio import fwrite
import sys

#
# generate signal
#
sr = 48000.0

signal = n.zeros((1500,))

def sin_at(sr, freq):
    n_samples = sr/freq
    return n.sin(n.linspace(0, 2*n.pi, n_samples))[:-1]


for i in [ord(x) for x in 'KaMaSu']:
    print "10 cycles of %dHz" % i
    for j in range(5):
        signal = n.hstack((signal, sin_at(sr, i)))

signal = n.hstack((signal, n.zeros((1500,))))

writable_signal = signal.astype(n.float32)

print "Writing %u samples of %u bytes to signal.dat" % (writable_signal.size,
                                                        writable_signal.itemsize)

fd = open('signal.dat', 'wb')
fwrite(fd, writable_signal.size, writable_signal)

예제 #18
0
print "resmax = ", n.max(results), " mean ", n.mean(results)
print "resshape", results.shape

#print outfft.astype(n.float32)
# spectral()

# pcolor(x_labels, y_labels, results.T)

Z = 10*np.log10(results.astype(n.float32))
# reverses in time
# Z = n.flipud(Z)

print "Z.size is ", Z.size
print "Z.shape is ", Z.shape

try:
    os.unlink(sys.argv[2])
    print "removed ", sys.argv[2]
except:
    print sys.argv[2], " doesnt exist"

outfd = open(sys.argv[2], 'wb')
fwrite(outfd, 2, n.array(Z.shape).astype(n.float32))

fwrite(outfd, Z.size, Z)

# imshow(Z, None, extent = (x_labels[0], x_labels[-1],y_labels[0], y_labels[-1]))
# 
# gca().axis('tight')
# show()
예제 #19
0
		# for plot
		line.set_ydata(Ez)
		draw()
	'''

	#---------------------------------------------
	# for time average
	field_tsum[:] += Ez[:]

	if last_period == 'on':
		field2_tsum += Ez[:]**2 

		# write the binary file
		filename = dirname + '/' + 'Ez_%.6d.scbin' % (tstep)
		fd	=	open(filename, 'wb')
		fwrite(fd, Nx, Ez)
		fd.close()

	#---------------------------------------------
	if (tstep/cap_t*cap_t == tstep and tstep != 0) or tstep == TMAX-1:
		t1 = time()
		elapse_time = localtime(t1-t0-60*60*9)
		str_time = strftime('[%j]%H:%M:%S', elapse_time)
		#print '%s    tstep = %d' % (str_time, tstep)
		
		# for plot
		line.set_ydata(Ez)
		draw()

		# for time average
		field_tavg[:] = field_tsum[:]/period
예제 #20
0
    for outer in range(0, len(a)-(winlen*2)):

        corr_coeff = n.zeros((winlen,))
        for inner in range(winlen):
            periodic_autocorr = sum(a[outer:outer+winlen]
                                    * a[outer+inner:outer+inner+winlen]) / winlen
            corr_coeff[inner] = periodic_autocorr

        delta_corr = corr_coeff[1:] - corr_coeff[0:-1]
        for i in range(int((sr/200)/4), len(delta_corr)-1):
            if delta_corr[i] >= 0 and delta_corr[i+1] < 0:
                # print "@", outer, sr/(i+2), " Hz"
                freqs[outer] = sr/(i+2)
                break

        if outer % 2 == 0:
            print "%d%% %f Hz                 \r" % ((100 * outer)/(len(a)-(winlen*2)),
                                                     freqs[outer]),
            
    return freqs

#
# doit
#

f = autocorrelate(signal).astype(n.float32)

fd = open('freqs.python.dat', 'wb')
fwrite(fd, f.size, f)