Commit 51e36042 authored by Monica Rainer's avatar Monica Rainer
Browse files

Fix BERV computation

parent 3840fc17
import numpy as np
def idlMod(a, b):
"""
Emulate 'modulo' behavior of IDL.
Parameters
----------
a : float or array
Numerator
b : float
Denominator
Returns
-------
IDL modulo : float or array
The result of IDL modulo operation.
"""
if isinstance(a, np.ndarray):
s = np.sign(a)
m = np.mod(a, b)
m[(s < 0)] -= b
else:
m = a % b
if a < 0: m -= b
return m
from .pyaValErrs import *
from .pyaOtherErrors import *
from .warn import *
from __future__ import print_function
import copy
import six
import traceback
class PyaErrTemplate(Exception):
def __init__(self, what, errType, where=None, why=None, solution=None, addInfo=None, tbfe=None):
"""
The PyA error class template.
Parameters
----------
what : string
What has happened?
errType : string
Description of the type of error.
where : string
Where did it happen?
why : string
Why did it happen?
solution : string or list of strings
How can the problem be solved?
addInfo : string
Whatever additional information may be available.
tbfe : Exception
Saves trace back from a previously raised exception.
Of the parameters, only the first (what) and second (errType) are mandatory; the latter
should be provided by every more specialized exception class.
.. note:: The template should never be raised directly, but specialized derived classes
should be used.
"""
self.errorType = errType
self.what = what
self.where = where
self.why = why
# Check whether solution is iterable
self.solution = solution
if solution is not None:
if not isinstance(solution, six.string_types):
self.solution = copy.deepcopy(solution)
else:
self.solution = [solution]
self.addInfo = addInfo
# Check trace back
if not tbfe is None:
self.addTB(tbfe)
else:
self.tbfe = None
def addTB(self, e):
"""
Add trace back from another exception.
Parameters
----------
e : Exception
The exception from which to add.
"""
self.tbfe = [" - " + l for l in traceback.format_exc().splitlines(True)]
def __str__(self, head=True):
"""
Return error message as string.
Parameters:
- `head` - boolean, If True, a header will be printed says "PyA error". Setting False is, e.g.,
used in printing warnings.
"""
e = ""
if head:
e = "\n"
e += "---------------------\n"
e += "A PyA error occurred:\n"
e += "---------------------\n"
if not self.tbfe is None:
# Print information from trace back (add indentation)
e += "PyA trace back:\n"
for l in self.tbfe:
e += l
if head:
e += "Type of error: " + self.errorType + "\n"
e += "What happened?\n"
e += " " + self.what + "\n"
if not self.where is None:
e += "Where did it happen?\n"
e += " " + self.where + "\n"
if not self.why is None:
e += "Why did it happen?\n"
e += " " + self.why + "\n"
if not self.solution is None:
e += "What are possible solutions?\n"
for s in self.solution:
e += " - " + s + "\n"
if not self.addInfo is None:
e += "Additional information:\n"
e += " " + self.addInfo + "\n"
return e
from __future__ import absolute_import
from .pyaErrTemplate import PyaErrTemplate
class PyAOrderError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever operations seem to be carried out in the
wrong order.
"""
PyaErrTemplate.__init__(self, what, "PyA Order Error", **keys)
class PyAParameterConflict(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
This exception should be raised when conflicting/mutually exclusive
parameters are received.
"""
PyaErrTemplate.__init__(self, what, "PyA Parameter Conflict", **keys)
class PyANetworkError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
This exception should be raised when an action through the network
has failed (e.g., a download).
"""
PyaErrTemplate.__init__(self, what, "PyA Network Error", **keys)
class PyANotImplemented(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
This exception should be raised when the function/method called has not been implemented. \
Such a situation often occurs when a member is to be implemented in a derived class \
(abstract base class concept in c++).
"""
PyaErrTemplate.__init__(self, what, "PyA Not Implemented", **keys)
class PyAImportFailure(PyaErrTemplate, ImportError):
def __init__(self, what, **keys):
"""
When to be raised?
When an import fails. This may not be fatal.
"""
PyaErrTemplate.__init__(self, what, "PyA import failure", **keys)
class PyARequiredImport(PyaErrTemplate, ImportError):
def __init__(self, what, **keys):
"""
When to be raised?
If a definitely needed package (e.g., numpy) cannot be imported.
"""
PyaErrTemplate.__init__(self, what, "PyA import error", **keys)
class PyAUnclassifiedError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
If an error occurred that cannot or shall not be specified further.
"""
PyaErrTemplate.__init__(self, what, "PyA unclassified error", **keys)
class PyADownloadError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
A download could not successfully be carried out.
"""
PyaErrTemplate.__init__(self, what, "PyA download error", **keys)
class PyAFileError(PyaErrTemplate):
def __init__(self, fn, mode, **keys):
"""
When to be raised?
Whenever a file-related error is detected.
Parameters
----------
fn : string
Name of the file in question.
mode : string, {no, ne}
- "no": File could not be opened.
- "ne": File does not exist
"""
if mode == "no":
what = "File '" + str(fn) + "' could not be opened."
elif mode == "ne":
what = "File '" + str(fn) + "' does not exist."
PyaErrTemplate.__init__(self, what, "PyA file error", **keys)
class PyAAlgorithmFailure(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever an algorithm fails in accomplishing its purpose.
"""
PyaErrTemplate.__init__(self, what, "PyA algorithm failure", **keys)
class PyADeprecationError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever a deprecated part of code is called.
"""
PyaErrTemplate.__init__(self, what, "PyA deprecation note", **keys)
from __future__ import absolute_import
from .pyaErrTemplate import PyaErrTemplate
class PyAValError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever an unexpected value is encountered.
"""
PyaErrTemplate.__init__(self, what, "PyA Value Error", **keys)
class PyANameClash(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever an unexpected doubling of names (e.g., key values in a dictionary) is encountered.
"""
PyaErrTemplate.__init__(self, what, "PyA Name Clash", **keys)
class PyAFloatingPointError(PyaErrTemplate):
def __init__(self, what, **keys):
"""
When to be raised?
Whenever a floating point error is caught.
"""
PyaErrTemplate.__init__(self, what, "PyA Floating Point Error", **keys)
from __future__ import print_function
from __future__ import absolute_import
from .pyaErrTemplate import PyaErrTemplate
def warn(w):
"""
Parameters:
- `w` - Something which can be converted into a string.
This may especially be a PyA exception, which is treated as a warning here.
Warnings are printed to stdout.
"""
print("--------------------")
print("| PyA User warning |")
print("--------------------")
if isinstance(w, PyaErrTemplate):
print(w.__str__(head=False))
return
print(str(w))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment