Python user guide, writing data


Install python wrapper using pip from your prefered python interpreter :

pip install python-unsio -U

unsio.output module for writing data

Unsio python wrapper has a module called unsio.output that you have to import to your python program. Example :

import unsio.output as uns_out

Writing data

Writing UNSIO data from python language is pretty simple and is done in 3 steps:

  1. UNSIO object instantiation
  2. Setting data to save
  3. Saving data

Create/instantiate CUNS_OUT object

Unsio python wrapper is based on C++ API with slight modifications to works with numpy arrays. Module unsio.output provides an easy way to save snapshot data, in different format, from a set of methods implemented in CUNS_OUT object. The first step is to instantiate a CUNS_OUT object.

import unsio.input as uns_in
import unsio.output as uns_out


# input
uns = uns_in.CUNS_IN(simname,components,times,float32)

# output
outfile="mysnap.g2"     # output filename
type="gadget2"             # we choose to save file in gadget2 format
unsout=uns_out.CUNS_OUT(outfile,type,float32)  # instantiate output object

CUNS_OUT constructor accepts 3 parameters :

  1. outfile, is a string, which contain the output file name
  2. type, is a string which contain the format type in which you want to save (see the list of supported output files )
  3. float32, is a boolean to activate (True -default) for saving data in single precision / deactivate (False ) for saving data in double precision

Setting data

CUNS_OUT class provides one universal function to set data to be stored in a file : CUNS_OUT.setData( data_array, comp, tag)

Setting numpy arrays

CUNS_OUT.setData(data_array, comp, tag=None)

   data_array (array|float) : 1D numpy_array or single value

   comp (str) : component gas,stars,disk,halo,bndry or bulge

   tag    (str) : array to set
                  example :

IMPORTANT : if 'tag' is None, then 'select' is treated as 'tag' (see above)

Return :
    status : 1 if success, 0 otherwise

From argument comp , we specify the component to be treated.

component value description
gas Gas particles
halo Dark matter particles
dm Dark matter particles
disk Old stars particles
stars Stars particles
bulge Bulge particles
bndry bndry particles

From argument tag , we specify which data we want to set.

tag string descripton numpy data_array passed as parameter
pos particles positions numpy 1D array of particles position (size=n*3)
vel particles velocities numpy 1D array of particles velocitie (size=n*3)
mass particles masses numpy 1D array of particles velocitie (size=n)
acc particles accelerations numpy 1D array of particles acceleration (size=n*3)
pot particles potential numpy 1D array of particles potential (size=n)
rho particles densities numpy 1D array of particles density (size=n)
hsml particles hsml numpy 1D array of particles hydro smooth length (size=n)
temp particles temperatures numpy 1D array of particles temperature (size=n)
age particles ages numpy 1D array of particles age (size=n)
metal particles metallicity numpy 1D array of particles metallicity (size=n)
u particles internal energy numpy 1D array of particles internal energy (size=n)
aux particles auxilliary (NEMO) numpy 1D auxiliary array (size=n)
keys particles keys (NEMO) numpy 1D keys array (size=n)
id particles indexes (NEMO) numpy 1D keys array (size=n)

Setting single value

  • setData(data_value,tag)

tag specify which data value we want to set

Known tag values are :

tag string descripton float or integer value passed as parameter
time snapshot time float value

Saving data

Once you have set all your data, you must call save() method to write file on disk

A complete example

To see a complete example, follow this link.