/
zoom-cache-gnuplot.hs
111 lines (95 loc) · 3.58 KB
/
zoom-cache-gnuplot.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{-# LANGUAGE ExistentialQuantification #-}
module Main (
main
) where
import Data.Maybe (fromMaybe, catMaybes)
import System.Environment (getArgs)
import System.Console.GetOpt
import Graphics.Gnuplot.Simple
import Graphics.Gnuplot.Advanced
import Graphics.Gnuplot.Value.Tuple (C(..))
import Data.ZoomCache.Read (getTrackType, getCacheFile)
import Data.Iteratee.ZoomCache (Stream)
import Data.ZoomCache.Common (TrackType(..), TrackNo, TimeStamp)
import Data.ZoomCache.Gnuplot
data ParseError = ParseError
parseTrack :: String -> Either ParseError (FilePath, TrackNo, Int)
parseTrack arg =
case w of
[w1, w2, w3] -> Right (w1, read w2, read w3)
_ -> Left ParseError
where
w = words arg
words :: String -> [String]
words s = case dropWhile (==':') s of
"" -> []
s' -> w : words s''
where
(w, s'') = break (==':') s'
-- Options record, only gnuplot options for now
data Options = Options
{ gnuplotOpts :: [Attribute]
, candleSticks :: [(FilePath, TrackNo, Int)]
, avgs :: [(FilePath, TrackNo, Int)]
}
defaultOptions = Options
{ gnuplotOpts = []
, candleSticks = []
, avgs = []
}
parseCustom :: String -> Attribute
parseCustom s =
Custom s1 [tail s2]
where (s1, s2) = break (==':') s
options :: [OptDescr (Options -> Options)]
options =
[ Option ['g'] ["gnuplot"]
(OptArg ((\ f opts -> opts { gnuplotOpts = parseCustom f : gnuplotOpts opts }) . fromMaybe "gnuplot")
"KEY:VALUE")
"gnuplot KEY:VALUE"
, Option ['c'] ["candlesticks"]
(OptArg ((\ f opts ->
opts { candleSticks = either (error "bad command line syntax")
(: candleSticks opts) $ parseTrack f }) .
fromMaybe "candlesticks")
"FILE:TRACKNO:SUMMARYLVL")
"candelsticks FILE:TRACKNO:SUMMARYLVL"
]
parseOpts :: [String] -> IO (Options, [String])
parseOpts argv =
case getOpt Permute options argv of
(o, n, []) -> return (foldl (flip id) defaultOptions o, n)
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo header options))
where header = "Usage: zoom-cache-gnuplot ..."
data SomeC = forall a. C a => SomeC a
instance C SomeC where
text (SomeC a) = text a
main :: IO ()
main = do
args <- getArgs
(opts, remainder) <- parseOpts args
-- mapM_ (process opts) remainder
cPlots <- fmap catMaybes . mapM candleProcess $ candleSticks opts
let plots = cPlots
case plots of
[] -> error "Cannot produce empty plot"
_ -> plotListsStyle (gnuplotOpts opts) plots
where
candleProcess :: (FilePath, TrackNo, Int) -> IO (Maybe (PlotStyle, [(TimeStamp, (Double, Double, Double, Double))]))
candleProcess (fp, tn, lvl) = do
cf <- getCacheFile fp
case getTrackType tn cf of
Just ZInt -> do
streams <- getStreams fp tn :: IO [Stream Int]
let (s, l) = candlePlots streams lvl
let l' = map (\(t,(a,b,c,d)) -> (t, ( realToFrac a
, realToFrac b
, realToFrac c
, realToFrac d)
:: (Double, Double, Double, Double))) l
return $ Just (s, l')
Just ZDouble -> do
streams <- getStreams fp tn :: IO [Stream Double]
let (s, l) = candlePlots streams lvl
return $ Just (s, l)
Nothing -> return Nothing