-- Convention used below:
-- when 'name' and 'nameCT' both appear, 'name' is the Haskell function and
-- 'nameCT' is the "Code Template" that 'name' builds.

module Drasil.GlassBR.ModuleDefs (allMods, implVars, interpY, interpZ) where

import Language.Drasil (QuantityDict, Space(..), implVar, nounPhraseSP,
  label, sub, HasSymbol(..), HasUID, Symbol)
import Language.Drasil.Display (Symbol(..))
import Language.Drasil.ShortHands
import Language.Drasil.Code (($:=), Func, FuncStmt(..), Mod, 
  asVC, funcDef, fDecDef, ffor, funcData, quantvar, 
  multiLine, packmod, repeated, singleLine)
import Language.Drasil.CodeExpr
import qualified Drasil.GlassBR.Unitals as U
import Language.Drasil.Printers


allMods :: [Mod]
allMods :: [Mod]
allMods = [Mod
readTableMod, Mod
interpMod]

-- It's a bit odd that this has to be explicitly built here...
implVars :: [QuantityDict]
implVars :: [QuantityDict]
implVars = [QuantityDict
v, QuantityDict
x_z_1, QuantityDict
y_z_1, QuantityDict
x_z_2, QuantityDict
y_z_2, QuantityDict
mat, QuantityDict
col,
  QuantityDict
i, QuantityDict
j, QuantityDict
k, QuantityDict
z, QuantityDict
zVector, QuantityDict
yMatrix, QuantityDict
xMatrix, QuantityDict
y, QuantityDict
arr, QuantityDict
filename,
  QuantityDict
y_2, QuantityDict
y_1, QuantityDict
x_2, QuantityDict
x_1, QuantityDict
x]

--from TSD.txt:

readTableMod :: Mod
readTableMod :: Mod
readTableMod = Name -> Name -> [Class] -> [Func] -> Mod
packmod Name
"ReadTable"
  Name
"Provides a function for reading glass ASTM data" [] [Func
readTable]

readTable :: Func
readTable :: Func
readTable = Name -> Name -> DataDesc -> Func
funcData Name
"read_table"
  Name
"Reads glass ASTM data from a file with the given file name"
  [ LinePattern -> Delim -> Data
singleLine ([DataItem] -> LinePattern
repeated [forall c. (Quantity c, MayHaveUnit c) => c -> DataItem
quantvar QuantityDict
zVector]) Delim
',',
    LinePattern -> Delim -> Data
multiLine ([DataItem] -> LinePattern
repeated (forall a b. (a -> b) -> [a] -> [b]
map forall c. (Quantity c, MayHaveUnit c) => c -> DataItem
quantvar [QuantityDict
xMatrix, QuantityDict
yMatrix])) Delim
','
  ]

-----

one, two :: Symbol
one :: Symbol
one = Int -> Symbol
Integ Int
1
two :: Symbol
two = Int -> Symbol
Integ Int
2

var :: String -> String -> Symbol -> Space -> QuantityDict
var :: Name -> Name -> Symbol -> Space -> QuantityDict
var Name
nam Name
np Symbol
symb Space
sp = Name -> NP -> Space -> Symbol -> QuantityDict
implVar Name
nam (Name -> NP
nounPhraseSP Name
np) Space
sp Symbol
symb

y_2, y_1, x_2, x_1, x :: QuantityDict
y_1 :: QuantityDict
y_1  = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"y1" Name
"lower y-coordinate"             (Symbol -> Symbol -> Symbol
sub Symbol
lY Symbol
one) Space
Real
y_2 :: QuantityDict
y_2  = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"y2" Name
"upper y-coordinate"             (Symbol -> Symbol -> Symbol
sub Symbol
lY Symbol
two) Space
Real
x_1 :: QuantityDict
x_1  = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"x1" Name
"lower x-coordinate"             (Symbol -> Symbol -> Symbol
sub Symbol
lX Symbol
one) Space
Real
x_2 :: QuantityDict
x_2  = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"x2" Name
"upper x-coordinate"             (Symbol -> Symbol -> Symbol
sub Symbol
lX Symbol
two) Space
Real
x :: QuantityDict
x    = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"x"  Name
"x-coordinate to interpolate at" Symbol
lX           Space
Real -- = params.wtnt from mainFun.py

v, x_z_1, y_z_1, x_z_2, y_z_2, mat, col,
  i, j, k, z, zVector, yMatrix, xMatrix, y, arr, filename :: QuantityDict
i :: QuantityDict
i = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"i" Name
"index" Symbol
lI           Space
Natural
j :: QuantityDict
j = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"j" Name
"index" Symbol
lJ           Space
Natural
k :: QuantityDict
k = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"k" Name
"index" (Symbol -> Symbol -> Symbol
sub Symbol
lK Symbol
two) Space
Natural     
v :: QuantityDict
v = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"v" Name
"value whose index will be found" Symbol
lV Space
Real
y :: QuantityDict
y = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"y" Name
"y-coordinate to interpolate at"  Symbol
lY Space
Real
z :: QuantityDict
z = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"z" Name
"z-coordinate to interpolate at"  Symbol
lZ Space
Real

zVector :: QuantityDict
zVector = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"zVector" Name
"list of z values" 
  (Symbol -> Symbol -> Symbol
sub Symbol
lZ (Name -> Symbol
label Name
"vector")) (Space -> Space
Vect Space
Real)               
yMatrix :: QuantityDict
yMatrix = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"yMatrix" Name
"lists of y values at different z values" 
  (Symbol -> Symbol -> Symbol
sub Symbol
lY (Name -> Symbol
label Name
"matrix")) (Space -> Space
Vect forall a b. (a -> b) -> a -> b
$ Space -> Space
Vect Space
Real)        
xMatrix :: QuantityDict
xMatrix = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"xMatrix" Name
"lists of x values at different z values" 
  (Symbol -> Symbol -> Symbol
sub Symbol
lX (Name -> Symbol
label Name
"matrix")) (Space -> Space
Vect forall a b. (a -> b) -> a -> b
$ Space -> Space
Vect Space
Real)        
arr :: QuantityDict
arr     = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"arr"     Name
"array in which value should be found" 
  (Name -> Symbol
label Name
"arr")             (Space -> Space
Vect Space
Real)  --FIXME: temporary variable for findCT?
x_z_1 :: QuantityDict
x_z_1   = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"x_z_1"   Name
"list of x values at a specific z value"    
  (Symbol -> Symbol -> Symbol
sub Symbol
lX (Symbol -> Symbol -> Symbol
sub Symbol
lZ Symbol
one))      (Space -> Space
Vect Space
Real)
y_z_1 :: QuantityDict
y_z_1   = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"y_z_1"   Name
"list of y values at a specific z value"    
  (Symbol -> Symbol -> Symbol
sub Symbol
lY (Symbol -> Symbol -> Symbol
sub Symbol
lZ Symbol
one))      (Space -> Space
Vect Space
Real)   
x_z_2 :: QuantityDict
x_z_2   = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"x_z_2"   Name
"list of x values at a specific z value"    
  (Symbol -> Symbol -> Symbol
sub Symbol
lX (Symbol -> Symbol -> Symbol
sub Symbol
lZ Symbol
two))      (Space -> Space
Vect Space
Real)
y_z_2 :: QuantityDict
y_z_2   = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"y_z_2"   Name
"list of y values at a specific z value"   
  (Symbol -> Symbol -> Symbol
sub Symbol
lY (Symbol -> Symbol -> Symbol
sub Symbol
lZ Symbol
two))      (Space -> Space
Vect Space
Real)
mat :: QuantityDict
mat     = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"mat"     Name
"matrix from which column will be extracted"     
  (Name -> Symbol
label Name
"mat")             (Space -> Space
Vect forall a b. (a -> b) -> a -> b
$ Space -> Space
Vect Space
Real)
col :: QuantityDict
col     = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"col"     Name
"extracted column"    
  (Name -> Symbol
label Name
"col")             (Space -> Space
Vect Space
Real)               
filename :: QuantityDict
filename = Name -> Name -> Symbol -> Space -> QuantityDict
var Name
"filename" Name
"name of file with x y and z data" 
  (Name -> Symbol
label Name
"filename")        Space
String

------------------------------------------------------------------------------------------
--
-- Some semantic functions

-- Given two points (x1,y1) and (x2,y2), return the slope of the line going through them
slope :: (CodeExpr, CodeExpr) -> (CodeExpr, CodeExpr) -> CodeExpr
slope :: (CodeExpr, CodeExpr) -> (CodeExpr, CodeExpr) -> CodeExpr
slope (CodeExpr
x1, CodeExpr
y1) (CodeExpr
x2, CodeExpr
y2) = (CodeExpr
y2 forall r. ExprC r => r -> r -> r
$- CodeExpr
y1) forall r. ExprC r => r -> r -> r
$/ (CodeExpr
x2 forall r. ExprC r => r -> r -> r
$- CodeExpr
x1)

-- Given two points (x1,y1) and (x2,y2), and an x ordinate, return
-- extrapoled y on the straight line in between
onLine :: (CodeExpr, CodeExpr) -> (CodeExpr, CodeExpr) -> CodeExpr -> CodeExpr
onLine :: (CodeExpr, CodeExpr)
-> (CodeExpr, CodeExpr) -> CodeExpr -> CodeExpr
onLine p1 :: (CodeExpr, CodeExpr)
p1@(CodeExpr
x1, CodeExpr
y1) (CodeExpr, CodeExpr)
p2 CodeExpr
x_ = (CodeExpr
m forall r. ExprC r => r -> r -> r
`mulRe` (CodeExpr
x_ forall r. ExprC r => r -> r -> r
$- CodeExpr
x1)) forall r. ExprC r => r -> r -> r
`addRe` CodeExpr
y1
                 where m :: CodeExpr
m = (CodeExpr, CodeExpr) -> (CodeExpr, CodeExpr) -> CodeExpr
slope (CodeExpr, CodeExpr)
p1 (CodeExpr, CodeExpr)
p2

------------------------------------------------------------------------------------------
-- Code Template helper functions

vLook :: (HasSymbol a, HasSymbol i, HasUID a, HasUID i) => a -> i -> CodeExpr -> CodeExpr
vLook :: forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook a
a i
i_ CodeExpr
p = forall r. ExprC r => r -> r -> r
idx (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy a
a) (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy i
i_ forall r. ExprC r => r -> r -> r
`addI` CodeExpr
p)

aLook :: (HasSymbol a, HasSymbol i, HasSymbol j, HasUID a, HasUID i, HasUID j) =>
  a -> i -> j -> CodeExpr
aLook :: forall a i j.
(HasSymbol a, HasSymbol i, HasSymbol j, HasUID a, HasUID i,
 HasUID j) =>
a -> i -> j -> CodeExpr
aLook a
a i
i_ j
j_ = forall r. ExprC r => r -> r -> r
idx (forall r. ExprC r => r -> r -> r
idx (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy a
a) (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy i
i_)) (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy j
j_)

getCol :: (HasSymbol a, HasSymbol i, HasUID a, HasUID i) => a -> i -> CodeExpr -> CodeExpr
getCol :: forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol a
a_ i
i_ CodeExpr
p = forall r f. (ExprC r, HasUID f, HasSymbol f) => f -> [r] -> r
apply (Func -> QuantityDict
asVC Func
extractColumnCT) [forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy a
a_, forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy i
i_ forall r. ExprC r => r -> r -> r
`addI` CodeExpr
p]

call :: Func -> [QuantityDict] -> FuncStmt
call :: Func -> [QuantityDict] -> FuncStmt
call Func
f [QuantityDict]
l = CodeExpr -> FuncStmt
FVal forall a b. (a -> b) -> a -> b
$ forall r f. (ExprC r, HasUID f, HasSymbol f) => f -> [r] -> r
apply (Func -> QuantityDict
asVC Func
f) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy [QuantityDict]
l

find :: (HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) => zv -> z -> CodeExpr
find :: forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find zv
zv z
z_ = forall r f. (ExprC r, HasUID f, HasSymbol f) => f -> [r] -> r
apply (Func -> QuantityDict
asVC Func
findCT) [forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy zv
zv, forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy z
z_]

linInterp :: [CodeExpr] -> CodeExpr
linInterp :: [CodeExpr] -> CodeExpr
linInterp = forall r f. (ExprC r, HasUID f, HasSymbol f) => f -> [r] -> r
apply (Func -> QuantityDict
asVC Func
linInterpCT)

interpOver :: (HasUID ptx, HasUID pty, HasUID ind, HasUID vv,
  HasSymbol ptx, HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
  ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver :: forall ptx pty ind vv.
(HasUID ptx, HasUID pty, HasUID ind, HasUID vv, HasSymbol ptx,
 HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver ptx
ptx pty
pty ind
ind vv
vv =
  [ forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook ptx
ptx ind
ind (forall r. LiteralC r => Integer -> r
int Integer
0), forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook pty
pty ind
ind (forall r. LiteralC r => Integer -> r
int Integer
0)
  , forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook ptx
ptx ind
ind (forall r. LiteralC r => Integer -> r
int Integer
1), forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook pty
pty ind
ind (forall r. LiteralC r => Integer -> r
int Integer
1)
  , forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy vv
vv ]
------------------------------------------------------------------------------------------
-- Code Templates

-- Note how this one uses a semantic function in its body
-- But it is also 'wrong' in the sense that it assumes x_1 <= x <= x_2
linInterpCT :: Func
linInterpCT :: Func
linInterpCT = forall c.
(Quantity c, MayHaveUnit c) =>
Name -> Name -> [c] -> Space -> Maybe Name -> [FuncStmt] -> Func
funcDef Name
"lin_interp" Name
"Performs linear interpolation" 
  [QuantityDict
x_1, QuantityDict
y_1, QuantityDict
x_2, QuantityDict
y_2, QuantityDict
x] Space
Real (forall a. a -> Maybe a
Just Name
"y value interpolated at given x value")
  [ CodeExpr -> FuncStmt
FRet forall a b. (a -> b) -> a -> b
$ (CodeExpr, CodeExpr)
-> (CodeExpr, CodeExpr) -> CodeExpr -> CodeExpr
onLine (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
x_1, forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_1) (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
x_2, forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_2) (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
x) ]

findCT :: Func
findCT :: Func
findCT = forall c.
(Quantity c, MayHaveUnit c) =>
Name -> Name -> [c] -> Space -> Maybe Name -> [FuncStmt] -> Func
funcDef Name
"find" 
  Name
"Finds the array index for a value closest to the given value" 
  [QuantityDict
arr, QuantityDict
v] Space
Natural (forall a. a -> Maybe a
Just Name
"index of given value in given array")
  [
    forall c.
(Quantity c, MayHaveUnit c) =>
c -> CodeExpr -> [FuncStmt] -> FuncStmt
ffor QuantityDict
i (forall r. ExprC r => r -> r
dim (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
arr) forall r. ExprC r => r -> r -> r
$- forall r. LiteralC r => Integer -> r
int Integer
1)
      [ CodeExpr -> [FuncStmt] -> [FuncStmt] -> FuncStmt
FCond ((forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
arr QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0) forall r. ExprC r => r -> r -> r
$<= forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
v) forall r. ExprC r => r -> r -> r
$&& (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
v forall r. ExprC r => r -> r -> r
$<= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
arr QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1)))
        [ CodeExpr -> FuncStmt
FRet forall a b. (a -> b) -> a -> b
$ forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
i ] [] ],
    Name -> FuncStmt
FThrow Name
"Bound error"
  ]

extractColumnCT :: Func
extractColumnCT :: Func
extractColumnCT = forall c.
(Quantity c, MayHaveUnit c) =>
Name -> Name -> [c] -> Space -> Maybe Name -> [FuncStmt] -> Func
funcDef Name
"extractColumn" Name
"Extracts a column from a 2D matrix" 
  [QuantityDict
mat, QuantityDict
j] (Space -> Space
Vect Space
Real) (forall a. a -> Maybe a
Just Name
"column of the given matrix at the given index")
  [
    forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
col (forall r. ExprC r => [[r]] -> r
matrix [[]]),
    --
    forall c.
(Quantity c, MayHaveUnit c) =>
c -> CodeExpr -> [FuncStmt] -> FuncStmt
ffor QuantityDict
i (forall r. ExprC r => r -> r
dim (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
mat))
      [ CodeExpr -> CodeExpr -> FuncStmt
FAppend (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
col) (forall a i j.
(HasSymbol a, HasSymbol i, HasSymbol j, HasUID a, HasUID i,
 HasUID j) =>
a -> i -> j -> CodeExpr
aLook QuantityDict
mat QuantityDict
i QuantityDict
j) ],
    CodeExpr -> FuncStmt
FRet (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
col)
  ]

interpY :: Func
interpY :: Func
interpY = forall c.
(Quantity c, MayHaveUnit c) =>
Name -> Name -> [c] -> Space -> Maybe Name -> [FuncStmt] -> Func
funcDef (forall x. HasSymbol x => x -> Name
showHasSymbImpl QuantityDict
U.interpY)
  Name
"Linearly interpolates a y value at given x and z values" 
  [QuantityDict
filename, QuantityDict
x, QuantityDict
z] Space
Real (forall a. a -> Maybe a
Just Name
"y value interpolated at given x and z values")
  [
  -- hack
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
xMatrix (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
yMatrix (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
zVector (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  --
  Func -> [QuantityDict] -> FuncStmt
call Func
readTable [QuantityDict
filename, QuantityDict
zVector, QuantityDict
xMatrix, QuantityDict
yMatrix],
  -- endhack
    QuantityDict
i     forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find QuantityDict
zVector QuantityDict
z,
    QuantityDict
x_z_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
xMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0),
    QuantityDict
y_z_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
yMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0),
    QuantityDict
x_z_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
xMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1),
    QuantityDict
y_z_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
yMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1),
    [FuncStmt] -> [FuncStmt] -> FuncStmt
FTry
      [ QuantityDict
j forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find QuantityDict
x_z_1 QuantityDict
x,
        QuantityDict
k forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find QuantityDict
x_z_2 QuantityDict
x ]
      [ Name -> FuncStmt
FThrow Name
"Interpolation of y failed" ],
    QuantityDict
y_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= [CodeExpr] -> CodeExpr
linInterp (forall ptx pty ind vv.
(HasUID ptx, HasUID pty, HasUID ind, HasUID vv, HasSymbol ptx,
 HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver QuantityDict
x_z_1 QuantityDict
y_z_1 QuantityDict
j QuantityDict
x),
    QuantityDict
y_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= [CodeExpr] -> CodeExpr
linInterp (forall ptx pty ind vv.
(HasUID ptx, HasUID pty, HasUID ind, HasUID vv, HasSymbol ptx,
 HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver QuantityDict
x_z_2 QuantityDict
y_z_2 QuantityDict
k QuantityDict
x),
    CodeExpr -> FuncStmt
FRet forall a b. (a -> b) -> a -> b
$ [CodeExpr] -> CodeExpr
linInterp [ forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
zVector QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0), forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_1, forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
zVector QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1), forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_2, forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
z ]
  ]

interpZ :: Func
interpZ :: Func
interpZ = forall c.
(Quantity c, MayHaveUnit c) =>
Name -> Name -> [c] -> Space -> Maybe Name -> [FuncStmt] -> Func
funcDef (forall x. HasSymbol x => x -> Name
showHasSymbImpl QuantityDict
U.interpZ)
  Name
"Linearly interpolates a z value at given x and y values" 
  [QuantityDict
filename, QuantityDict
x, QuantityDict
y] Space
Real (forall a. a -> Maybe a
Just Name
"z value interpolated at given x and y values")
  [
    -- hack
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
xMatrix (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
yMatrix (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
fDecDef QuantityDict
zVector (forall r. ExprC r => [[r]] -> r
matrix [[]]),
  --
  Func -> [QuantityDict] -> FuncStmt
call Func
readTable [QuantityDict
filename, QuantityDict
zVector, QuantityDict
xMatrix, QuantityDict
yMatrix],
  -- endhack
    forall c.
(Quantity c, MayHaveUnit c) =>
c -> CodeExpr -> [FuncStmt] -> FuncStmt
ffor QuantityDict
i (forall r. ExprC r => r -> r
dim (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
zVector) forall r. ExprC r => r -> r -> r
$- forall r. LiteralC r => Integer -> r
int Integer
1)
      [
        QuantityDict
x_z_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
xMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0),
        QuantityDict
y_z_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
yMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0),
        QuantityDict
x_z_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
xMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1),
        QuantityDict
y_z_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
getCol QuantityDict
yMatrix QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1),
        [FuncStmt] -> [FuncStmt] -> FuncStmt
FTry
          [ QuantityDict
j forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find QuantityDict
x_z_1 QuantityDict
x,
            QuantityDict
k forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= forall zv z.
(HasUID zv, HasUID z, HasSymbol zv, HasSymbol z) =>
zv -> z -> CodeExpr
find QuantityDict
x_z_2 QuantityDict
x ]
          [ FuncStmt
FContinue ],
        QuantityDict
y_1 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= [CodeExpr] -> CodeExpr
linInterp (forall ptx pty ind vv.
(HasUID ptx, HasUID pty, HasUID ind, HasUID vv, HasSymbol ptx,
 HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver QuantityDict
x_z_1 QuantityDict
y_z_1 QuantityDict
j QuantityDict
x),
        QuantityDict
y_2 forall c. (Quantity c, MayHaveUnit c) => c -> CodeExpr -> FuncStmt
$:= [CodeExpr] -> CodeExpr
linInterp (forall ptx pty ind vv.
(HasUID ptx, HasUID pty, HasUID ind, HasUID vv, HasSymbol ptx,
 HasSymbol pty, HasSymbol ind, HasSymbol vv) =>
ptx -> pty -> ind -> vv -> [CodeExpr]
interpOver QuantityDict
x_z_2 QuantityDict
y_z_2 QuantityDict
k QuantityDict
x),
        CodeExpr -> [FuncStmt] -> [FuncStmt] -> FuncStmt
FCond ((forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_1 forall r. ExprC r => r -> r -> r
$<= forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y) forall r. ExprC r => r -> r -> r
$&& (forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y forall r. ExprC r => r -> r -> r
$<= forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_2))
          [ CodeExpr -> FuncStmt
FRet forall a b. (a -> b) -> a -> b
$ [CodeExpr] -> CodeExpr
linInterp [ forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_1, forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
zVector QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
0), forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y_2, forall a i.
(HasSymbol a, HasSymbol i, HasUID a, HasUID i) =>
a -> i -> CodeExpr -> CodeExpr
vLook QuantityDict
zVector QuantityDict
i (forall r. LiteralC r => Integer -> r
int Integer
1), forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy QuantityDict
y ]
          ] []
      ],
    Name -> FuncStmt
FThrow Name
"Interpolation of z failed"
  ]

interpMod :: Mod
interpMod :: Mod
interpMod = Name -> Name -> [Class] -> [Func] -> Mod
packmod Name
"Interpolation" 
  Name
"Provides functions for linear interpolation on three-dimensional data" []
  [Func
linInterpCT, Func
findCT, Func
extractColumnCT, Func
interpY, Func
interpZ]