mirror of
https://gitlab.dit.htwk-leipzig.de/computermusik-ws23/data-composition.git
synced 2025-12-15 23:44:17 +01:00
Upload current attempts
This commit is contained in:
44
scratchpads/csv.hs
Normal file
44
scratchpads/csv.hs
Normal file
@@ -0,0 +1,44 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
-- This requires my custom BootTidal.hs
|
||||
|
||||
import Prelude hiding (filter)
|
||||
import qualified Data.ByteString.Lazy as BL
|
||||
import Data.Csv -- this is from the Cassava library
|
||||
import qualified Data.Vector as V
|
||||
-- import System.Directory (doesFileExist)
|
||||
-- import Text.Printf
|
||||
-- import Control.Monad (forM_)
|
||||
import GHC.IO.Exception
|
||||
import Control.Exception (try)
|
||||
|
||||
|
||||
data TimestampedDatum = TimestampedDatum {
|
||||
timestamp :: String,
|
||||
datum :: Float
|
||||
} deriving Show
|
||||
|
||||
dataPath = "../data/weather/balcony_temperature.csv"
|
||||
|
||||
instance FromNamedRecord TimestampedDatum where
|
||||
parseNamedRecord r =
|
||||
TimestampedDatum
|
||||
<$> r .: "time"
|
||||
<*> r .: "C.value"
|
||||
|
||||
|
||||
type ErrorMsg = String
|
||||
type CsvData = (Header, V.Vector TimestampedDatum)
|
||||
|
||||
parseCsv :: FilePath -> IO (Either ErrorMsg CsvData)
|
||||
parseCsv filePath = do
|
||||
result <- try $ BL.readFile filePath
|
||||
return $ case result of
|
||||
Left (exception :: IOException) -> Left $ show exception
|
||||
Right contents -> decodeByName contents
|
||||
|
||||
getList :: CsvData -> V.Vector TimestampedDatum
|
||||
getList = snd
|
||||
|
||||
-- getValue :: TimestampedDatum -> Float
|
||||
-- getValue = datum
|
||||
Reference in New Issue
Block a user