Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inmap: Add isnan and isinf output functions #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ type Outputter struct {
// 'log10(x)' which applies the base-10 logarithm function log10(e).
//
// 'sum(x)' which sums a variable across all grid cells.
//
// 'isnan(x)' which reports whether x is an IEEE 754 “not-a-number” value.
//
// 'isinf(x, sign)' which reports whether x is an infinity, according to sign.
// If sign > 0, isinf reports whether x is positive infinity. If sign < 0,
// isinf reports whether x is negative infinity. If sign == 0, isinf reports
// whether x is either infinity.
func NewOutputter(fileName string, allLayers bool, outputVariables map[string]string, outputFunctions map[string]govaluate.ExpressionFunction, m Mechanism) (*Outputter, error) {
defaultOutputFuncs := map[string]govaluate.ExpressionFunction{
"exp": func(arg ...interface{}) (interface{}, error) {
Expand All @@ -471,6 +478,18 @@ func NewOutputter(fileName string, allLayers bool, outputVariables map[string]st
}
return floats.Sum(arg[0].([]float64)), nil
},
"isnan": func(arg ...interface{}) (interface{}, error) {
if len(arg) != 1 {
return nil, fmt.Errorf("inmap: got %d arguments for function 'isnan', but need 1", len(arg))
}
return math.IsNaN(arg[0].(float64)), nil
},
"isinf": func(arg ...interface{}) (interface{}, error) {
if len(arg) != 2 {
return nil, fmt.Errorf("inmap: got %d arguments for function 'isinf', but need 2", len(arg))
}
return math.IsInf(arg[0].(float64), arg[1].(int)), nil
},
}

for key, val := range outputFunctions {
Expand Down