The UFF class

class pyuff.UFF(filename=None, fileName=None)[source]

Manages data reading and writing from/to the UFF file.

The UFF class instance requires exactly 1 parameter - a file name of a universal file. If the file does not exist, no basic file info will be extracted and the status will be False - indicating that the file is not refreshed. Hovewer, when one tries to read one or more data-sets, the file must exist or the Exception will be raised.

The file, given as a parameter to the UFF instance, is open only when reading from or writing to the file. The UFF instance refreshes the file automatically - use UFF.get_status() to see the refresh status); note that this works fine if the file is being changed only through the UFF instance and not by other functions or even by other means, e.g., externally. If the file is changed externally, the UFF.refresh() should be invoked before any reading or writing.

All array-type data are read/written using numpy’s np.array module.

__init__(filename=None, fileName=None)[source]

Initializes the uff object and extract the basic info: the number of sets, types of the sets and format of the sets (ascii or binary). To manually refresh this info, call the refresh method manually.

Whenever some data is written to a file, a read-only flag indicates that the file needs to be refreshed - before any reading, the file is refreshed automatically (when needed).

get_supported_sets()[source]

Returns a list of data-sets supported for reading and writing.

get_n_sets()[source]

Returns the number of valid sets found in the file.

get_set_types()[source]

Returns an array of data-set types. All valid data-sets are returned, even those that are not supported, i.e., whose contents will not be read.

get_set_formats()[source]

Returns an array of data-set formats: 0=ascii, 1=binary.

get_file_name()[source]

Returns the file name (as a string) associated with the uff object.

file_exists()[source]

Returns true if the file exists and False otherwise. If the file does not exist, invoking one of the read methods would raise the Exception exception.

get_status()[source]

Returns the file status, i.e., True if the file is refreshed and False otherwise.

refresh()[source]

Extract/refreshes the info of all the sets from UFF file (if the file exists). The file must exist and must be accessable otherwise, an error is raised. If the file cannot be refreshed, False is returned and True otherwise.

read_sets(setn=None)[source]

Reads sets from the list or array setn. If setn=None, all sets are read (default). Sets are numbered starting at 0, ending at n-1. The method returns a list of dset dictionaries - as many dictionaries as there are sets. Unknown data-sets are returned empty.

User must be sure that, since the last reading/writing/refreshing, the data has not changed by some other means than through the UFF object.

write_sets(dsets, mode='add', force_double=True)[source]

Writes several UFF data-sets to the file. The mode can be either ‘add’ (default) or ‘overwrite’. The dsets is a list of dictionaries, each representing one data-set. Unsupported data-sets will be ignored. When only 1 data-set is to be written, no lists are necessary, i.e., only one dictionary is required.

For each data-set, there are some optional and some required fields at dset dictionary. Also, in general, the sum of the required and the optional fields together can be less then the number of fields read from the same type of data-set; the reason is that for some data-sets some fields are set automatically. Optional fields are calculated automatically and the dset is updated - as dset is actually an alias (aka pointer), this is reflected at the caller too.

The required fields are:

dset - dictionary representing the data-set mode - ‘add’ or ‘overwrite’ force_double - True or False (default True). Single precision should be avoided,

therefore by default data is saved with double precision.