File reading/writing utility

Overview

This module provides functions for file input/output. These are all wrapper functions, based on existing functions in other Python classes. Functions are provided to save a two-dimensional array to a text file, load selected columns of data from a text file, load a column header line, compact strings to include only legal filename characters, and a function from the Python Cookbook to recursively match filename patterns.

See the __main__ function for examples of use.

Module functions

pyradi.ryfiles.saveHeaderArrayTextFile(filename, dataArray, header=None, comment=None, delimiter=None)

Save a numpy array to a file, included header lines.

This function saves a two-dimensional array to a text file, with an optional user-defined header. This functionality will be part of numpy 1.7, when released.

Args:
filename (string): name of the output ASCII flatfile.
dataArray (np.array[N,M]): a two-dimensional array.
header (string): the optional header.
comment (string): the symbol used to comment out lines, default value is None.
delimiter (string): delimiter used to separate columns, default is whitespace.
Returns:
Nothing.
Raises:
No exception is raised.
pyradi.ryfiles.loadColumnTextFile(filename, loadCol=[1], comment=None, normalize=0, skiprows=0, delimiter=None, abscissaScale=1, ordinateScale=1, abscissaOut=None)

Load selected column data from a text file, processing as specified.

This function loads column data from a text file, manipulating the data read in. The individual vector data must be given in columns in the file, with the abscissa (x-value) in first column (col 0 in Python) and any number of ordinate (y-value) vectors in second and later columns.

Note: leave only single separators (e.g. spaces) between columns! Also watch out for a single sapce at the start of line.

Args:
filename (string): name of the input ASCII flatfile.
loadCol ([int]): the M =len([]) column(s) to be loaded as the ordinate, default value is column 1
comment (string): string, the symbol used to comment out lines, default value is None
normalize (int): integer, flag to indicate if data must be normalized.
skiprows (int): integer, the number of rows to be skipped at the start of the file (e.g. headers)
delimiter (string): string, the delimiter used to separate columns, default is whitespace.
abscissaScale (float): scale by which abscissa (column 0) must be multiplied
ordinateScale (float): scale by which ordinate (column >0) must be multiplied
abscissaOut (np.array[N,] or [N,1]): abscissa vector on which output variables are interpolated.
Returns:
(np.array[N,M]): The interpolated, M columns of N rows, processed array.
Raises:
No exception is raised.
pyradi.ryfiles.loadHeaderTextFile(filename, loadCol=[1], comment=None)

Loads column data from a text file, using the csv package.

Using the csv package, loads column header data from a file, from the firstrow. Headers must be delimited by commas. The function [LoadColumnTextFile] provides more comprehensive capabilties.

Args:
filename (string): the name of the input ASCII flatfile.
loadCol ([int]): list of numbers, the column headers to be loaded , default value is column 1
comment (string): the symbol to comment out lines
Returns:
[string]: a list with selected column header entries
Raises:
No exception is raised.
pyradi.ryfiles.cleanFilename(sourcestring, removestring=u' %:/, .\\[]')

Clean a string by removing selected characters.

Creates a legal and ‘clean’ sourcestring from a string by removing some clutter and illegals. A default set is given but the user can override the default string.

Args:
sourcestring (string): the string to be cleaned.
removestring (string): remove all these characters from the source.
Returns:
(string): A cleaned-up string.
Raises:
No exception is raised.
pyradi.ryfiles.listFiles(root, patterns=u'*', recurse=1, return_folders=0, useRegex=False)

Lists the files/directories meeting specific requirement

Searches a directory structure along the specified path, looking for files that matches the glob pattern. If specified, the search will continue into sub-directories. A list of matching names is returned.

Args:
root (string): root directory from where the search must take place
patterns (string): glob pattern for filename matching
recurse (unt): should the search extend to subdirs of root?
return_folders (int): should foldernames also be returned?
useRegex (bool): should regular expression evaluation be used?
Returns:
A list with matching file/directory names
Raises:
No exception is raised.
pyradi.ryfiles.readRawFrames(fname, rows, cols, vartype, loadFrames=[])

Constructs a numpy array from data in a binary file with known data-type.

Args:
fname (string): path and filename
rows (int): number of rows in frames
cols (int): number of columns in frames
vartype (numpy.dtype): numpy data type of data to be read
int8, int16, int32, int64
uint8, uint16, uint32, uint64
float16, float32, float64
loadFrames ([int]): optional list of frames to load, zero-based , empty list (default) loads all frames
Returns:
frames (int) : number of frames in the returned data set,
0 if error occurred
rawShaped (numpy.ndarray): vartype numpy array of dimensions (frames,rows,cols),
None if error occurred
Raises:
No exception is raised.
pyradi.ryfiles.arrayToLaTex(filename, arr, header=None, leftCol=None, formatstring=u'%1.4e', filemode=u'wt')

Write a numpy array to latex table format in output file.

The table can contain only the array data (no top header or left column side-header), or you can add either or both of the top row or side column headers. Leave ‘header’ or ‘leftcol’ as None is you don’t want these.

The output format of the array data can be specified, i.e. scientific notation or fixed decimal point.

Args:
fname (string): output path and filename
arr (np.array[N,M]): array with table data
header (string): column header in final latex format
leftCol ([string]): left column each row, in final latex format
formatstring (string): output format precision for array data (see numpy.savetxt)
filemode (string): file open mode (a=append, w=new file)
Returns:
None, writes a file to disk
Raises:
No exception is raised.
pyradi.ryfiles.epsLaTexFigure(filename, epsname, caption, scale, filemode=u'a')
Write the code to include an eps graphic as a latex figure.
The text is added to an existing file.
Args:
fname (string): output path and filename
epsname (string): filename/path to eps file
caption (string): figure caption
scale (double): scale to textwidth [0..1]
filemode (string): file open mode (a=append, w=new file)
Returns:
None, writes a file to disk
Raises:
No exception is raised.
pyradi.ryfiles.read2DLookupTable(filename)

Read a 2D lookup table and extract the data.

The table has the following format:

line 1: xlabel ylabel title
line 2: 0 (vector of y (col) abscissa)
lines 3 and following: (element of x (row) abscissa), followed
by table data.

From line/row 3 onwards the first element is the x abscissa value followed by the row of data, one point for each y abscissa value. The format can depicted as follows:

x-name y-name ordinates-name
0 y1 y2 y3 y4
x1 v11 v12 v13 v14
x2 v21 v22 v23 v24
x3 v31 v32 v33 v34
x4 v41 v42 v43 v44
x5 v51 v52 v53 v54
x6 v61 v62 v63 v64

This function reads the file and returns the individual data items.

Args:
fname (string): input path and filename
Returns:
xVec ((np.array[N])): x abscissae
yVec ((np.array[M])): y abscissae
data ((np.array[N,M])): data corresponding the x,y
xlabel (string): x abscissa label
ylabel (string): y abscissa label
title (string): dataset title
Raises:
No exception is raised.

Table Of Contents

Previous topic

Planck and thermal radiation

Next topic

Plotting utility

This Page