the iofunc module

pNbody.iofunc.ReadArray(f, data_type, shape=None, byteorder='little', pio='no', nlocal=None, htype=<class 'numpy.int32'>)

data_type = int,float32,float or data_type = array

shape = tuple

pNbody.iofunc.ReadBlock(f, data_type, shape=None, byteorder='little', pio='no', htype=<class 'numpy.int32'>)

data_type = int,float32,float or data_type = array

shape = tuple

pioparallel io, ‘yes’ or ‘no’

if ‘yes’, each proc read each file if ‘no’, proc 0 read and send to each other

pNbody.iofunc.ReadDataBlock(f, data_type, shape=None, byteorder='little', pio='no', npart=None, skip=False)

Read a block containg data. If NTask = 1 or pio = ‘yes’, the block is read normally. If NTask > 1 and pio = ‘no’, the master reads the block and send the data to the slaves.

In the second case :

  1. the master send N/Ntask element to each task.

  2. if the var npart is present, he send Np/Ntask to each task, for each Np of npart.

data_type = array

shape = tuple

pNbody.iofunc.ReadFits(filename)

Read a fits file.

pNbody.iofunc.ReadYaml(file_name, parameter_name=None, encoding='latin-1')

read a yaml file and return a dictionary

pNbody.iofunc.WriteArray(f, data, byteorder='little', pio='no', npart=None, htype=<class 'numpy.int32'>)

data = array

shape = tuple

pNbody.iofunc.WriteBlock(f, data, byteorder='little', htype=<class 'numpy.int32'>)

data = ((x,float32),(y,int),(z,float32),(label,40))

shape = tuple

pNbody.iofunc.WriteDataBlock(f, data, byteorder='little', pio='no', npart=None)

Write a block containg data. If NTask = 1 or pio = ‘yes’, the block is written normally. If NTask > 1 and pio = ‘no’, the master get the block from the slaves and write it.

In the second case :

  1. the master get N/Ntask element from each task.

  2. if the var npart is present, he get Np/Ntask from each task, for each Np of npart.

data = array

shape = tuple

pNbody.iofunc.WriteFits(data, filename, extraHeader=None)

Write a fits file

pNbody.iofunc.checkfile(name)

Check if a file exists. An error is generated if the file does not exists.

Parameters

name (the path to a filename) –

Examples

>>> io.checkfile('an_existing_file')
>>>
>>> io.checkfile('a_non_existing_file')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/epfl/revaz/local/lib64/python2.6/site-packages/pNbody/io.py", line 33, in checkfile
    raise IOError(915,'file %s not found ! Pease check the file name.'%(name))
IOError: [Errno 915] file nofile not found ! Pease check the file name.
pNbody.iofunc.end_of_file(f, pio='no', MPI=None)

Return True if we have reached the end of the file f, False instead

Parameters
  • f (ndarray or matrix object) – an open file

  • pio ('yes' or 'no') – if the file is read in parallel or not

  • MPI (MPI communicator) –

Returns

status – True if the we reached the end of the file False if not

Return type

Bool

pNbody.iofunc.old_WriteFits(data, filename, extraHeader=None)

Write a fits file

pNbody.iofunc.read_ascii(file, columns=None, lines=None, dtype=<class 'numpy.float32'>, skipheader=False, cchar='#')

Read an ascii file. The function allows to set the number of columns or line to read. If it contains a header, the header is used to label all column. In this case, a dictionary is returned.

Parameters
  • file (the path to a file or an open file) –

  • columns (list) – the list of the columns to read if none, all columns are read

  • lines (list) – the list of the lines to read if none, all lines are read

  • dtype (dtype) – the ndtype of the objects to read

  • skipheader (bool) – if true, do not read the header if there is one

  • cchar (char) – lines begining with cchar are skiped the first line is considered as the header

Returns

data – A python dictionary or an ndarray object

Return type

Dict or ndarray

Examples

>>> from numpy import *
>>> x = arange(10)
>>> y = x*x
>>> f = open('afile.txt','w')
>>> f.write("# x y")
>>> for i in xrange(len(x)):
... f.write('%g %g'%(x[i],y[i]))
...
>>> f.close()
>>> from pNbody import iofunc as io
>>> data = io.read_ascii("afile.txt")
>>> data['x']
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> data['y']
array([  0.,   1.,   4.,   9.,  16.,  25.,  36.,  49.,  64.,  81.])
pNbody.iofunc.read_cooling(file)

Read cooling file

pNbody.iofunc.read_dmp(file)

Read a dmp (pickle) file.

Parameters

file (the path to a file) –

Returns

data

Return type

a python object

Examples

>>> x = {'a':1,'b':2}
>>> io.write_dmp('/tmp/afile.dmp',x)
>>> y = io.read_dmp('/tmp/afile.dmp')
>>> y
{'a': 1, 'b': 2}
pNbody.iofunc.read_params(file)

Read params Gadget file and return the content in a dictionary

pNbody.iofunc.readblock(f, data_type, shape=None, byteorder='little', skip=False, htype=<class 'numpy.int32'>)

data_type = int,float32,float or data_type = array

shape = tuple

class pNbody.iofunc.sdict

a slightly extended dictionnary class

insert(entry)

insert a new entry the entry must be a dictionary.

selectc(c)

return a subset of the dictionnary with only elements where the condition is true

pNbody.iofunc.write_array(file, vec)

Write an array to a file, in a very simple ascii format.

Parameters
  • file (the path to a file) –

  • vec (an ndarray object) –

Examples

>>> from numpy import *
>>> x = array([1,2,3])
>>> io.write_array('/tmp/array.dat',x)
pNbody.iofunc.write_dmp(file, data)

Write a dmp (pickle) file. In other word, dump the data object.

Parameters
  • file (the path to a file) –

  • data (a pickable python object) –

Examples

>>> x = {'a':1,'b':2}
>>> io.write_dmp('/tmp/afile.dmp',x)
pNbody.iofunc.writeblock(f, data, byteorder='little', htype=<class 'numpy.int32'>)

data = array or data = ((x,float32),(y,int),(z,float32),(label,40))

shape = tuple