-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTideAmplitudes.hs
58 lines (40 loc) · 1.58 KB
/
TideAmplitudes.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
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
#if IN_TEST_HARNESS
module TideAmplitudes where
import Prelude hiding (IO, print, putStr, putStrLn)
import System.IO.Fake
#endif
import TCD
import TCDExtra
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import System.Environment (getArgs, getProgName)
import System.Exit (die)
import Text.Printf
main :: IO ()
main = liftIO getArgs >>= \case
[station, date] -> do
num <- maybe (liftIO $ die "Cannot find station") pure =<< searchDbsForStation station
hdr <- getTideDbHeader
let nConstituents = fromIntegral (hdrConstituents hdr) :: Int
baseYear = fromIntegral (hdrStartYear hdr) :: Int
yearNum = read date - baseYear
indices = [0..nConstituents-1]
nodeFactors <- mapM (`getNodeFactor` yearNum) indices
(rn, r) <- readTideRecord num
unless (rn == num) $ error "Cannot read record"
let amplitudes = trAmplitudes r
offset = trDatumOffset r
putStrLn " i Amplitude Factor Result"
putStrLn "--- --------- ------ ------"
forM_ (zip3 indices amplitudes nodeFactors) $ \(i, a, f) ->
when (a /= 0.0) $
putStrLn $ printf "%3d %6.4f * %6.4f = %6.4f" (i :: Int) a f (a * f)
let maxamp = innerProduct amplitudes nodeFactors
m2ft = (/ 0.3048)
putStrLn $ printf "Maximum amplitude: %.6f - %.6f = %.6f = %.6fft"
(offset+maxamp) (offset-maxamp) (2*maxamp) (m2ft $ 2*maxamp)
_ -> liftIO $ die . printf "Usage: %s STATION DATE" =<< getProgName
where
innerProduct xs ys = sum $ zipWith (*) xs ys