pencil.io.npfile

Class for reading and writing numpy arrays from / to binary files

Attributes

sys_endian_code

Classes

npfile

Class for reading and writing numpy arrays to/from files

Module Contents

pencil.io.npfile.sys_endian_code = '<'
class pencil.io.npfile.npfile(file_name, permission='rb', endian='dtype', order='C')

Bases: object

Class for reading and writing numpy arrays to/from files

Inputs:
file_name – The complete path name to the file to open

or an open file-like object

permission – Open the file with given permissions: (‘r’, ‘w’, ‘a’)

for reading, writing, or appending. This is the same as the mode argument in the builtin open command.

format – The byte-ordering of the file:

([‘native’, ‘n’], [‘ieee-le’, ‘l’], [‘ieee-be’, ‘B’]) for native, little-endian, or big-endian respectively.

endian   -- default endian code for reading / writing
order    -- default order for reading writing
Type:

‘C’ or ‘F’

file     -- file object containing read / written data
seek, tell, close  -- as for file objects
rewind             -- set read position to beginning of file
read_raw           -- read string data from file (read method of file)
write_raw          -- write string data to file (write method of file)
read_array         -- read numpy array from binary file data
write_array        -- write numpy array contents to binary file

Example use: >>> from StringIO import StringIO >>> import numpy as np >>> from scipy.io import npfile >>> arr = np.arange(10).reshape(5,2) >>> # Make file-like object (could also be file name) >>> my_file = StringIO() >>> npf = npfile(my_file) >>> npf.write_array(arr) >>> npf.rewind() >>> npf.read_array((5,2), arr.dtype) >>> npf.close() >>> # Or read write in Fortran order, Big endian >>> # and read back in C, system endian >>> my_file = StringIO() >>> npf = npfile(my_file, order=’F’, endian=’>’) >>> npf.write_array(arr) >>> npf.rewind() >>> npf.read_array((5,2), arr.dtype)

endian = 'dtype'
order = 'C'
get_endian()
set_endian(endian_code)
parse_endian(endian_code)

Returns valid endian code from wider input options

close()
seek(*args)
tell()
rewind(howmany=None)

Rewind a file to its beginning or by a specified amount.

read_raw(size=-1)

Read raw bytes from file as string.

write_raw(str)

Write string to file as raw bytes.

remaining_bytes()
write_array(data, endian=None, order=None)

Write to open file object the flattened numpy array data

Inputs data - numpy array or object convertable to array endian - endianness of written data

(can be None, ‘dtype’, ‘<’, ‘>’) (if None, get from self.endian)

order - order of array to write (C, F)

(if None from self.order)

read_array(dt, shape=-1, endian=None, order=None)

Read data from file and return it in a numpy array.

Inputs

dt - dtype of array to be read shape - shape of output array, or number of elements

(-1 as number of elements or element in shape means unknown dimension as in reshape; size of array calculated from remaining bytes in file)

endian - endianness of data in file

(can be None, ‘dtype’, ‘<’, ‘>’) (if None, get from self.endian)

order - order of array in file (C, F)

(if None get from self.order)

Outputs arr - array from file with given dtype (dt)

fort_write(data, endian=None, order=None, head_size=4)

Write a Fortran binary record from a numpy array

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.

fort_read(dt, shape=-1, endian=None, order=None, head_size=4)

Read data from a fortran binary record and return it in a numpy array.

note that fortran records give a 4-byte (or 8-byte if you use gfortran) header describing the number of bytes in a record. if shape does not agree with this, an error is raised.

Inputs

dt - dtype of array to be read shape - shape of output array, or number of elements

(-1 as number of elements or element in shape means unknown dimension as in reshape; size of array calculated from remaining bytes in file)

endian - endianness of data in file

(can be None, ‘dtype’, ‘<’, ‘>’) (if None, get from self.endian)

order - order of array in file (C, F)

(if None get from self.order)

head_size - the size in bytes of the fortran record header.

(almost always 4, except gfortran uses 8 by default)

Outputs arr - array from file with given dtype (dt)