utilities¶
These are tools or functions used to support the main computation.!!
__init__.py¶
blrb.py¶
meta.py¶
files.py¶
Provides utilities dealing with files.
- utilities.files.fl_config_file(extension='.ini', prefix='', level=None)[source]¶
Build a configuration filename (default extension .ini) based on the name and path of the function/module calling this function. Can also be useful for setting log file names automatically. If prefix is passed, this is preprended to the filename.
- Parameters
extension (str) – file extension to use (default ‘.ini’). The period (‘.’) must be included.
prefix (str) – Optional prefix to the filename (default ‘’).
level – Optional level in the stack of the main script (default = maximum level in the stack).
- Returns
Full path of calling function/module, with the source file’s extension replaced with extension, and optionally prefix inserted after the last path separator.
- Example
configFile = fl_config_file(‘.ini’) Calling fl_config_file from /foo/bar/baz.py should return /foo/bar/baz.ini
- utilities.files.fl_get_stat(filename, chunk_whole=65536)[source]¶
Get basic statistics of filename - namely directory, name (excluding base path), md5sum and the last modified date. Useful for checking if a file has previously been processed.
- Parameters
filename (str) – Filename to check.
chunk_whole (int) – (optional) chunk size (for md5sum calculation).
- Returns
path, name, md5sum, modification date for the file.
- Raises
TypeError – if the input file is not a string.
IOError – if the file is not a valid file, or if the file cannot be opened.
- Example
dir, name, md5sum, moddate = fl_get_stat(filename)
- utilities.files.fl_load_file(filename, comments='%', delimiter=',', skiprows=0)[source]¶
Load a delimited text file – uses
numpy.genfromtxt()- Parameters
filename (file or str) – File, filename, or generator to read
comments (str, optional) – (default ‘%’) indicator
delimiter (str, int or sequence, optional) – The string used to separate values.
- utilities.files.fl_log_fatal_error(tblines)[source]¶
Log the error messages normally reported in a traceback so that all error messages can be caught, then exit. The input ‘tblines’ is created by calling
traceback.format_exc().splitlines().- Parameters
tblines (list) – List of lines from the traceback.
- utilities.files.fl_mod_date(filename, dateformat='%Y-%m-%d %H:%M:%S')[source]¶
Return the last modified date of the input file
- Parameters
filename (str) – file name (full path).
dateformat (str) – Format string for the date (default ‘%Y-%m-%d %H:%M:%S’)
- Returns
File modification date/time as a string
- Return type
str
- Example
modDate = fl_mod_date( ‘C:/foo/bar.csv’ , dateformat=’%Y-%m-%dT%H:%M:%S’ )
- utilities.files.fl_module_name(level=1)[source]¶
Get the name of the module <level> levels above this function
- Parameters
level (int) – Level in the stack of the module calling this function (default = 1, function calling
fl_module_name)- Returns
Module name.
- Return type
str
- Example
mymodule = fl_module_name( ) Calling fl_module_name() from “/foo/bar/baz.py” returns “baz”
- utilities.files.fl_module_path(level=1)[source]¶
Get the path of the module <level> levels above this function
- Parameters
level (int) – level in the stack of the module calling this function (default = 1, function calling
fl_module_path)- Returns
path, basename and extension of the file containing the module
- Example
path, base, ext = fl_module_path( ), Calling fl_module_path() from “/foo/bar/baz.py” produces the result “/foo/bar”, “baz”, “.py”
- utilities.files.fl_program_version(level=None)[source]¶
Return the __version__ string from the top-level program, where defined.
If it is not defined, return an empty string.
- Parameters
level (int) – level in the stack of the main script (default = maximum level in the stack)
- Returns
version string (defined as the
__version__global variable)
- utilities.files.fl_save_file(filename, data, header='', delimiter=',', fmt='%.18e')[source]¶
Save data to a file.
Does some basic checks to ensure the path exists before attempting to write the file. Uses
numpy.savetxtto save the data.- Parameters
filename (str) – Path to the destination file.
data – Array data to be written to file.
header (str) – Column headers (optional).
delimiter (str) – Field delimiter (default ‘,’).
fmt (str) – Format statement for writing the data.
- utilities.files.fl_size(filename)[source]¶
Return the size of the input file in bytes
- Parameters
filename (str) – Full path to the file.
- Returns
File size in bytes.
- Return type
int
- Example
file_size = fl_size( ‘C:/foo/bar.csv’ )
- utilities.files.fl_start_log(log_file, log_level, verbose=False, datestamp=False, newlog=True)[source]¶
Start logging to log_file all messages of log_level and higher. Setting
verbose=Truewill report all messages to STDOUT as well.- Parameters
log_file (str) – Full path to log file.
log_level (str) – String specifiying one of the standard Python logging levels (‘NOTSET’,’DEBUG’,’INFO’,’WARNING’,’ERROR’, ‘CRITICAL’)
verbose (boolean) –
Truewill echo all logging calls to STDOUTdatestamp (boolean) –
Truewill include a timestamp of the creation time in the filename.newlog (boolean) –
Truewill create a new log file each time this function is called.Falsewill append to the existing file.
- Returns
logging.loggerobject.- Example
fl_start_log(‘/home/user/log/app.log’, ‘INFO’, verbose=True)
value_lookup.py¶
Contains lookup dictionaries for classification, e.g.
>>> terrain_class_desc = dict([(1, 'City Buildings'),
>>> (2, 'Dense Forest'),
>>> (3, 'High Density Metro'),
>>> ...
>>> (14, 'orched/open forest'),
>>> (15, 'Mudflats/saltevaporators/sandy beaches')])