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.
This package was partly developed to provide additional material in support of students and readers of the book Electro-Optical System Analysis and Design: A Radiometry Perspective, Cornelius J. Willers, ISBN 9780819495693, SPIE Monograph Volume PM236, SPIE Press, 2013. http://spie.org/x648.html?product_id=2021423&origin_id=x646
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.
Load selected column data from a text file, processing as specified.
This function loads column data from a text file, scaling and interpolating the read-in data, according to user specification. The first 0’th column has special significance: it is considered the abscissa (x-values) of the data set, while the remaining columns are any number of ordinate (y-value) vectors. The user passes a list of columns to be read (default is [1]) - only these columns are read, processed and returned when the function exits.The user also passes an abscissa vector to which the input data is interpolated and then subsequently amplitude scaled or normalised.
Note: leave only single separators (e.g. spaces) between columns! Also watch out for a single space at the start of line.
Loads column header data in the first string of a text file.
loads column header data from a file, from the first row. Headers must be delimited by commas. The function [LoadColumnTextFile] provides more comprehensive capabilties.
Clean a string by removing selected characters.
Creates a legal and ‘clean’ source string from a string by removing some clutter and characters not allowed in filenames. A default set is given but the user can override the default string.
Lists the files/directories meeting specific requirement
Returns a list of file paths to files in a file system, searching 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. The function supports a local or network reachable filesystem, but not URLs.
Loading multi-frame two-dimensional arrays from a raw data file of known data type.
The file must consist of multiple frames, all with the same number of rows and columns. Frames of different data types can be read, according to the user specification. The user can specify which frames must be loaded (if not the whole file).
Writes a single raw image frame to image file. The file type must be given, e.g. png or jpg. The image need not be scaled beforehand, it is done prior to writing out the image. Could be one of BMP, JPG, JPEG, PNG, PPM, TIFF, XBM, XPM) but the file types available depends on the QT imsave plugin in use.
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.
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 file 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 v64This function reads the file and returns the individual data items.
Download a file, given a URL.
The URL is used to download a file, to the saveFilename specified. If no saveFilename is given, the basename of the URL is used.
Unzip a file that was compressed using the gzip format.
The zipfilename is used to open a file, to the saveFilename specified. If no saveFilename is given, the basename of the zipfilename is used, but with the file extension removed.
Untar a tar archive, and save all files to the specified directory.
The tarfilename is used to open a file, extraxting to the saveDirname specified. If no saveDirname is given, the local directory ‘.’ is used.
Download and untar a compressed tar archive, and save all files to the specified directory.
The tarfilename is used to open the tar file, extracting to the destinationDir specified. If no destinationDir is given, the local directory ‘.’ is used. Before downloading, a check is done to determine if the file was already downloaded and exists in the local file system.