-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestTides.hs
209 lines (170 loc) · 8.36 KB
/
TestTides.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{-# LANGUAGE CPP #-}
{-# LANGUAGE NamedFieldPuns #-}
module Main where
import Tides
import Time
import Analysis
import Control.Arrow (second)
import Control.Monad (forM_, unless, when)
import Data.Bool (bool)
import Data.Function (on)
import Data.Time
import Data.Time.Zones (TZ, LocalToUTCResult(..), localTimeToUTCFull)
import System.Process (readCreateProcess, shell)
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
import Test.QuickCheck (Arbitrary(..), Property, choose, generate, sized)
import Test.QuickCheck.Monadic as QCM (assert, monadicIO, run)
import Text.ParserCombinators.ReadP (readP_to_S)
import Text.Printf (printf)
#if !MIN_VERSION_time(1,5,0)
import Data.Time.Locale.Compat (TimeLocale)
parseTimeOrError :: ParseTime t => Bool -> TimeLocale -> String -> String -> t
parseTimeOrError _ = readTime
readSTime :: ParseTime t => Bool -> TimeLocale -> String -> ReadS t
readSTime _ = readsTime
#endif
main :: IO ()
main = do
(location:args) <- getArgs
interval <- generate arbitrary
let zone = utc -- TODO: get from location
toLocal = clampTime . utcToLocalTime zone
clampTime (LocalTime d t) = LocalTime d t { todSec = 0 }
parseTime' :: ParseTime t => String -> String -> t
parseTime' = parseTimeOrError True defaultTimeLocale
toTime = parseTime' "%F %H:%M"
toInterval = realToFrac . timeOfDayToTime . parseTime' "%H:%M"
(begin, end, step) = if null args
then (toLocal $ prBegin interval, toLocal $ prEnd interval, prStep interval)
else (toTime $ args!!0, toTime $ args!!1, toInterval $ args!!2)
when (null args) $
putStrLn $ unwords [show begin, show end, show . timeToTimeOfDay . nominalToTime $ step]
predictionMismatches <- comparePredictions location begin end step
eventMismatches <- compareEvents location begin end step
unless (null predictionMismatches) $
putStrLn "Predictions don't match"
forM_ predictionMismatches $ \(a, b) ->
putStrLn $ formatPrediction a ++ " / " ++ formatPrediction b
unless (null eventMismatches) $
putStrLn "Events don't match"
forM_ eventMismatches $ \(a, b) ->
putStrLn $ formatEvent a ++ " / " ++ formatEvent b
bool exitFailure exitSuccess $ null predictionMismatches && null eventMismatches
comparePredictions :: String -> LocalTime -> LocalTime -> NominalDiffTime -> IO [(Prediction, Prediction)]
comparePredictions location begin end step = do
(predictions, _, _, tz) <- tides location begin end step
modelPredictions <- getModelPredictions tz location begin end step
let predictionPairs = zip modelPredictions predictions
eqPred (t, h) (t', h') = eqTime t t' && abs (h - h') < 1e-6
eqTime t t' = ztzName t == ztzName t' && abs (t `diffZonedTime` t') < 60
where ztzName = timeZoneName . zonedTimeZone
return $ filter (not . uncurry eqPred ) predictionPairs
prop_equalPredictions :: String -> PredictionInterval -> Property
prop_equalPredictions location interval = monadicIO $ do
let zone = utc -- TODO: get from location
toLocal = clampTime . utcToLocalTime zone
clampTime (LocalTime d t) = LocalTime d t { todSec = 0 }
(begin, end, step) = (toLocal $ prBegin interval, toLocal $ prEnd interval, prStep interval)
predictionMismatches <- QCM.run $ comparePredictions location begin end step
assert $ null predictionMismatches
compareEvents :: String -> LocalTime -> LocalTime -> NominalDiffTime -> IO [(Event, Event)]
compareEvents location begin end step = do
(_, events, _, tz) <- tides location begin end step
modelEvents <- getModelEvents tz location begin end
let eventPairs = zip modelEvents events
eqEvent (Extremum (t, h) c) (Extremum (t', h') c') = (c == c') && eqTime t t' && abs (h - h') < 0.006
eqTime t t' = ztzName t == ztzName t' && abs (t `diffZonedTime` t') <= 90
where ztzName = timeZoneName . zonedTimeZone
return $ filter (not . uncurry eqEvent) eventPairs
prop_equalEvents :: String -> PredictionInterval -> Property
prop_equalEvents location interval = monadicIO $ do
let zone = utc -- TODO: get from location
toLocal = clampTime . utcToLocalTime zone
clampTime (LocalTime d t) = LocalTime d t { todSec = 0 }
(begin, end, step) = (toLocal $ prBegin interval, toLocal $ prEnd interval, prStep interval)
eventMismatches <- QCM.run $ compareEvents location begin end step
assert $ null eventMismatches
formatPrediction :: Prediction -> String
formatPrediction (t, h) = printf "%s %9.6f" (formatTime defaultTimeLocale "%F %H:%M:%S %Z" t) h
formatEvent :: Event -> String
formatEvent (Extremum p c) = formatPrediction p ++ printf " %-4s Tide" (fmtXtType c)
getModelPredictions :: TZ -> String -> LocalTime -> LocalTime -> NominalDiffTime -> IO [Prediction]
getModelPredictions tz location begin end step = do
let cmd = tideCmd location begin end (Just step) "m"
map parseLine . lines <$> readCreateProcess (shell cmd) ""
where
parseLine = second read . parseXtTime tz
getModelEvents :: TZ -> String -> LocalTime -> LocalTime -> IO [Event]
getModelEvents tz location begin end = do
let cmd = tideCmd location begin end Nothing "p" ++ " | sed '1,/^$/d'"
map parseLine . lines <$> readCreateProcess (shell cmd) ""
where
parseLine s = Extremum (t, h) c
where (t, rest) = parseXtTime tz s
(ht, ty) = case words rest of
[w0, _, w2, _] -> (w0, w2)
_ -> error $ "Wrong number of words in line: " ++ s
h = read ht
c = parseXtType ty
tideCmd :: String -> LocalTime -> LocalTime -> Maybe NominalDiffTime -> String -> String
tideCmd location begin end step mode =
printf "tide -l '%s' -b '%s' -e '%s' -em pSsMm -m %s 2>/dev/null"
location (fmtXtTime begin) (fmtXtTime end) mode
++ maybe "" (printf " -s '%s'" . fmtXtInterval) step
fmtXtTime :: LocalTime -> String
fmtXtTime = formatTime defaultTimeLocale "%F %H:%M"
fmtXtInterval :: NominalDiffTime -> String
fmtXtInterval = formatTime defaultTimeLocale "%H:%M" . timeToTimeOfDay . realToFrac
readSXtTime :: TZ -> ReadS ZonedTime
readSXtTime tz = readP_to_S $ do
lt <- readPTime True defaultTimeLocale "%F %l:%M %p"
let knownTimeZones = case localTimeToUTCFull tz lt of
LTUUnique _ut z -> [z]
LTUAmbiguous _ut1 _ut2 z1 z2 -> [z1, z2]
LTUNone _ut z -> [z]
zonedLocale = defaultTimeLocale { knownTimeZones }
z <- readPTime True zonedLocale "%Z"
pure $ ZonedTime lt z
parseXtTime :: TZ -> String -> (ZonedTime, String)
parseXtTime tz s = case readSXtTime tz s of
[x] -> x
_ -> error $ "Can't parse time: " ++ s
fmtXtType :: Criticality -> String
fmtXtType Maximum = "High"
fmtXtType Minimum = "Low"
fmtXtType Inflection = "Stationary" -- Never happens?
parseXtType :: String -> Criticality
parseXtType t = case t of
"Low" -> Minimum
"High" -> Maximum
_ -> error $ "Unknown tide type: " ++ t
addZonedTime :: NominalDiffTime -> ZonedTime -> ZonedTime
addZonedTime ndt zt = utcToZonedTime' . addUTCTime ndt . zonedTimeToUTC $ zt
where utcToZonedTime' = utcToZonedTime (zonedTimeZone zt)
diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime
diffZonedTime = diffUTCTime `on` zonedTimeToUTC
data PredictionInterval = PredictionInterval
{ prBegin :: UTCTime
, prEnd :: UTCTime
, prStep :: NominalDiffTime
} deriving (Eq, Show)
instance Arbitrary PredictionInterval where
arbitrary = sized $ \number -> do
minutes <- choose (1, 120 :: Int)
begin <- choose (periodStart, periodEnd)
let step = realToFrac (minutes * 60)
end = min periodEnd $ (realToFrac number * step) `addUTCTime` begin
return $ PredictionInterval begin end step
where -- Period supported by current tide component database is 1700-2101
-- However, date code seems to have problems outside 1848-2037
periodStart = UTCTime (fromGregorian 1848 1 1) 0
periodEnd = UTCTime (fromGregorian 2037 1 1) (-1)
shrink (PredictionInterval b e s) =
if e `diffUTCTime` b > s
then map single [b, b `plus` s .. e `minus` s]
else []
where
single t = PredictionInterval t (t `plus` s) s
plus t d = addUTCTime d t
minus t d = addUTCTime (negate d) t