boutpy.boututils – useful tools

Table of Contents

calculus – Derivatives and Integrals

deriv(*args, **kwargs) Take derivative of 1D array.
integrate(var[, periodic]) Integrate a 1D array

Derivatives and integrals of periodic and non-periodic functions

B.Dudson, University of York, Nov 2009

boutpy.boututils.calculus.deriv(*args, **kwargs)[source]

Take derivative of 1D array.

result = deriv(y) result = deriv(x, y)

Parameters:
x, y : ndarray

If x is not given,

periodic : bool, optional, defalut: False

Domain is periodic if True.

Returns:
deriv : ndarray

Derivatives of 1D array.

boutpy.boututils.calculus.integrate(var, periodic=False)[source]

Integrate a 1D array

Return array is the same size as the input

compare_inp – parse & compare BOUT.inp

parser_config(configfile[, level1, header, …]) Parser config files.
dict_to_level1(Dict[, sep]) Convert a dict to a one level dict.
compare_inp(*configfiles, **kwargs) Comparison of configfiles, return & print details of differences.

Parser config files

  • parser configure files
  • convert a dict to a level1 dict
  • compare BOUT.inps
  • dataframe with ‘sec-opt’ multi_index for config file
boutpy.boututils.compare_inp.str_to_num(x)[source]

Convert str ‘x’ to int or float if possible, otherwise return itself.

boutpy.boututils.compare_inp.parser_config(configfile, level1=False, header=None, dataframe=False)[source]

Parser config files.

Return a dict contains all options information in the config files.

Parameters:
configfile : string

path to file or config string

level1 : bool, optional, default: False

If level1=False [default], then using ‘sec.opt’ as keys. Otherwise, Using all options as keys.

header : string, optional

section header for the beginning section if it doesn’t have one. Default is None which means it does not try to add header for the beginning section.

dataframe : bool, optional, default: False

return the result in pd.DataFrame format if dataframe=True.

Returns:
result : dict | pd.DataFrame

a dict with ‘sec.subsec[.subsubsec…].opt’ as key if level1 = True. Otherwise it is a multi-levels dict by default. return the result in pd.DataFrame format if dataframe=True.

Examples

>>> result = parser_config('/path/to/BOUT.inp')
# ConfigObj({'NOUT': '250', 'TIMESTEP': '1.e0',
# ...
# 'kappa': {'bndry_core': 'neumann', 'bndry_sol': 'neumann'}})
>>> result = parser_config('[header]\noption1=1', header='main')
# ConfigObj({'header': {'option1': '1'}})
>>> result = parser_config('#comments\noptions=1')
# ConfigObj({'options': '1'})
>>> result = parser_config('#comments\noptions=1', header='main')
# ConfigObj({'main': {'options': '1'}})
boutpy.boututils.compare_inp.dict_to_level1(Dict, sep=u'.')[source]

Convert a dict to a one level dict.

Parameters:
Dict : dict

its values may be in dict type

sep : char

a seperator to join keys in the multi-level dict

Returns:
result : dict

its values are not in dict type

Examples

>>> test1 = {'A': {'a': 1,'b': 2}, 'B': 3}
>>> dict_to_level1(test1)
#  {'A.a': 1, 'A.b': 2, 'B': 3}
>>> test2 = {'A':{'a': 1, 'b': 2}, 'a': 3}
>>> dict_to_level1(test2)
#  {'A.a': 1, 'A.b': 2, 'a': 3}
boutpy.boututils.compare_inp.multi_index(boutinp, header=u'global', column=u'Value')[source]

Return a panda.DataFrame with [‘Sec’, ‘Opt’] as multi-index.

Parameters:
boutinp : str

path to BOUT.inp file.

header : str, optional, default: ‘global’

the header name for the first section

column : str, optional, default: ‘Value’

column’s name

Returns:
df : pandas.DataFrame

pandas.DataFrame with multi-index

boutpy.boututils.compare_inp.compare_inp(*configfiles, **kwargs)[source]

Comparison of configfiles, return & print details of differences.

Parameters:
configfiles : list|tuple of stings

path of BOUT.inps

grid : bool, optional, default: False

output grid names if they are different.

quiet : bool, optional, default: False

print comparison result to screen.

case : bool, optional, default: True

case sensitive if case is True

short : bool, optional, default: True

Using short name for cases

render : bool, optional, default: True

highlight different values in terminal output.

Returns:
tuple(cases, table)
cases, table : pandas.DataFrame

cases : name of each case table : differences of each case

Examples

>>> import glob
>>> cases, table = compare_inp(glob.glob("*/BOUT.inp"), grid=False)

datafile – Netcdf File I/O

class boutpy.boututils.datafile.DataFile(filename=None, write=False, create=False, format='NETCDF3_CLASSIC')[source]

NETCDF file I/O class.

Methods

open(filename, write=False, create=False, format=’NETCDF3_CLASSIC’) Open NETCDF file
close() Close netcdf file
read(name, range=None) Read variable from file.

fileio – data I/O methods

save2nc(filename, mode, *args, **kwargs) Save variables to NetCDF file.
readsav(filename[, idict, python_dict, …]) Read an IDL .sav file.
file_import(filename) Import an NetCDF file.
file_list(filename[, ncol, sep]) Return a sorted list of names of all variables in NetCDF file.

This module is for data I/O

  • loading data from IDL .sav file
  • loading data from NetCDF file
  • saving data to NetCDF file

Requirements

  • datafile.DataFile
  • numpy.swapaxes
  • scipy.io.idl or idlsave
boutpy.boututils.fileio.save2nc(filename, mode, *args, **kwargs)[source]

Save variables to NetCDF file.

ONLY for numeric type variables.

Parameters:
filename : str
mode : str [‘w’,’a’]

the mode of dealing with the file. can be ‘write’(‘w’), ‘append’(‘a’), ‘append forcely’(‘af’)

  • ‘write’
    if filename does not exist, create one. if filename exist, erase content!!!!
  • ‘append’
    if filename not exist, create one if filename exist, append contents into the file.
info : bool

show extra details

format : str

describes the netCDF data model version, one of NETCDF3_CLASSIC, NETCDF4, NETCDF4_CLASSIC or NETCDF3_64BIT.

args : arguments, optional

Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside save2nc, the arrays will be saved with names “var_0”, “var_1”, and so on. These arguments can be any expression.

kwargs : keyword arguments, optional

Arrays to save to the file. Arrays will be saved in the file with the keyword names.

Returns:
None

See also

numpy.savez
Save several arrays into an uncompressed .npz file format
numpy.load
Load the files created by savez_compressed.

Notes

  • dims option: set name of dimensions
  • format option: set netCDF file model
  • 07/29/2016 created by J.G. Chen
  • 02/21/2017 by J.G.Chen
    initial fid to aviod Error when cannot open file.

Any bug reports and suggestion are gladly welcome. Email: cjgls@pku.edu.cn

boutpy.boututils.fileio.readsav(filename, idict=None, python_dict=False, uncompressed_file_name=None, verbose=False, reverse=True)[source]

Read an IDL .sav file.

NOTE : This function just reverses the axes order of the results
obtained from module scipy.io.idl.readsav() or idlsave.read(). reverse axes order: [x1, x2, …, xn] –> [xn, …, x2, x1]
Parameters:
file_name : str

Name of the IDL save file.

idict : dict, optional

Dictionary in which to insert .sav file variables

python_dict : bool, optional

By default, the object return is not a Python dictionary, but a case-insensitive dictionary with item, attribute, and call access to variables. To get a standard Python dictionary, set this option to True.

uncompressed_file_name : str, optional

This option only has an effect for .sav files written with the /compress option. If a file name is specified, compressed .sav files are uncompressed to this file. Otherwise, readsav will use the tempfile module to determine a temporary filename automatically, and will remove the temporary file upon successfully reading it in.

verbose : bool, optional

Whether to print out information about the save file, including the records read, and available variables.

reverse : bool, optional

reverse axes order of data

Returns:
data_dict : AttrDict or dict

If python_dict is set to False (default), this function returns a case-insensitive dictionary with item, attribute, and call access to variables. If python_dict is set to True, this function returns a Python dictionary with all variable names in lowercase. If idict was specified, then variables are written to the dictionary specified, and the updated dictionary is returned.

boutpy.boututils.fileio.file_import(filename)[source]

Import an NetCDF file.

Parameters:
filename : str

Name of NetCDF file.

Returns:
data : dict

This function returns a dictionary with variables name as its keys and its values are in numpy.ndarray type

boutpy.boututils.fileio.file_list(filename, ncol=None, sep=u' ')[source]

Return a sorted list of names of all variables in NetCDF file.

Parameters:
filename: str | dict

NetCDF file name or dict

ncol : int, optional, default: None

Output to stdout in ncol columns format, without return value.

sep : str, optional, default: ” “

Separator for the stdout.

Returns:
ret : list

return a name list if ncol is not specified.

functions – useful functions

get_yesno([prompt, default, timeout]) Get yes/no from input, return Ture/False correspondingly.
get_nth(n_th, end[, start, pad]) Get the nth value in closed interval [start, end]
get_limits(array) Get the minium and maxium value of array
get_input([prompt, timeout]) Get input from stdin with timer
nearest(array, target[, value]) Find the element in array which is closest to target
pycommand([default, lvars, gvars]) Run python command
nrange(start, stop, step[, endpoint]) Return evenly spaced numbers over a specified interval except endpoint: interval [start, start`+`step, …, stop]
sort_nicely(l) Sort the given list in the way that humans expect.

Common Funtions.

boutpy.boututils.functions.get_yesno(prompt=u'Yes?[y/N]: ', default=False, timeout=5)[source]

Get yes/no from input, return Ture/False correspondingly.

Parameters:
prompt : str

prompt string

default : bool

default if no input

timeout : int

return default value if no input after timeout second

Returns:
ret : bool

return True/False according to the input. if input is in [‘y’, ‘Y’, ‘yes’, ‘Yes’, ‘YES’, ‘T’, ‘True’], return True, if option in [‘n’, ‘N’, ‘no’, ‘No’, ‘NO’, ‘F’, ‘False’], return False, otherwise, return default

boutpy.boututils.functions.get_nth(n_th, end, start=0, pad=u'const')[source]

Get the nth value in closed interval [start, end]

Parameters:
start, end : int
n_th : scalar, 1D array-like of integer
pad : str [‘const’|’circle’]

if n_th > end-start+1:

  • pad=’const’: return the value at the nearest boundary
  • pad=’circle’: find the ‘n_th value in period array
    [start, …, end, …, start, …, end]
Returns:
result : scalar or 1d-ndarray integer

the n_th value in series ‘start, start+1, …, end’

boutpy.boututils.functions.get_limits(array)[source]

Get the minium and maxium value of array

Parameters:
array : scalar, array-like,
Returns:
vmin, vmax : float

minium and maxium value of array.

boutpy.boututils.functions.get_input(prompt=u'input', timeout=5)[source]

Get input from stdin with timer

Parameters:
prompt : str

prompt for input

timeout : int

timeout autometcically after timeout seconds

Returns:
ret : str or None

return input as str, None if no input

boutpy.boututils.functions.nearest(array, target, value=False)[source]

Find the element in array which is closest to target

Parameters:
array : array-like
target : scalar|array-like
value : bool, False by default

if True, then return the data closest to the target

Returns:
index : scalar, ndarray
data : scalar, ndarray
if `value` is True, return (index, data), return index (by default)
otherwise.
boutpy.boututils.functions.pycommand(default=None, lvars=None, gvars=None)[source]

Run python command

Parameters:
default : str, optional, default: None

default commands will be run first.

lvars, gvars : dict, optional, default: None

pass local and global vars to this function.

Returns:
None
boutpy.boututils.functions.sort_nicely(l)[source]

Sort the given list in the way that humans expect.

Parameters:
l : list, tuple
Returns:
result : list

Examples

>>> a=['z20', 'z2', 'z10']
>>> sort_nicely(a)
['z2', 'z10', 'z20']
>>> a.sort()
['z10', 'z2', 'z20']
boutpy.boututils.functions.nrange(start, stop, step, endpoint=True)[source]

Return evenly spaced numbers over a specified interval except endpoint: interval [start, start`+`step, …, stop]

The endpoint stop of the the interval can optionally be excluded.

Parameters:
start : scalar

The starting value of the sequence

stop : scalar

The end value of the sequence, unless endpoint is set to False.

step : scale

The spacing between samples.

endpoint : bool, optional

If True, stop is the last sample. Otherwise, it is not included. Default is True.

Returns:
samples : ndarray

Array of evenly spaced values.

[start, start+step, ..., start+n*step, stop] or [start, start+step, ..., start+n*step] where start+n*step < stop (depending on wether endpoint is True or False).

pfile – Osborne pfiles I/O

A class object used to interface with Osborne pfiles.

class boutpy.boututils.pfile.pfile(filename)[source]

A class object used to interface with Osborne pfiles.

This class is inheritated from collections.OrderedDict. Each variable are loaded as an dict item, its value is an dict object including keys ‘data’, ‘description’, ‘units’, ‘psinorm’, ‘derivative’.

Notes

This class is modified from omfit_osborne.py in the OMFIT project.

Attributes:
legacy : bool, default: False

If legacy=False, it means the pfile is a new-version which includes the information of impurities.

filename : str

File name of the pfile.

discriptions : dict

An dict contains discriptions string about each variable in the pfile.

units : dict

An dict contains units for each variable in the pfile.

Methods

load() load pfile data, called at the initiation stage.
save(filename=None) save pfile data to filename if it is given. Otherwise overwrite the original file.
plot() plot profiles in the pfile.
interpolate() interpolate the profiles to the same psinorm. Return all profiles and psin.
interpolate(npoints=500, psin=None, output=False)[source]

Interpolate profiles to same psin array.

Parameters:
npoints : int, default: 500

number of data points in interval [0, 1.0], if psin==None.

psin : ndarray, optional, default: None

interpolate profiles with given psin.

output : bool or str, optional, default: False

save data to netcdf file. if True, save data to file “pfilename`_nx`npoints.nc”. if it is an string, then save data to file “output.nc”

Returns:
data : dict

profiles with same psin.

load()[source]

Method used to load the content of the pfile.

plot()[source]

Method used to plot all profiles, one in a different subplots.

save(filename=None)[source]

Save content of the object to the file in Osborne pfile format.

Parameters:
filename : str, optional

if filename=None, it will overwrite the original pfile. otherwise it will use filename as file name.

shell – Run shell command

shell(command[, pipe]) Return the status and output of a shell command.
jobinfo(jobid) Return a dict containing all information of the slurm job.

Run a shell command.

boutpy.boututils.shell.shell(command, pipe=False)[source]

Return the status and output of a shell command.

Parameters:
command : str

Shell command

pipe : bool, optional, default: False.

Redirect the stdout as part of return if pipe=True.

Returns:
status : int

Exit code of shell command

output : str, None

stdout if pipe=True, otherwise None by default.

boutpy.boututils.shell.jobinfo(jobid)[source]

Return a dict containing all information of the slurm job.

Parameters:
jobid : str, int

Slurm jobid.

Returns:
jobinfo : dict, None

An dict cantains all information of the slurm job jobid. “None” if failed.