found rainiest period. cleaned up tests. adjusted a bit of formatting.
This commit is contained in:
parent
473dfa876e
commit
fa7cdeebec
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
(ns weather-data.rainiestday
|
(ns weather-data.rainiestday
|
||||||
(:require [clojure.data.csv :as csv]
|
(:require [clojure.data.csv :as csv]
|
||||||
[clojure.java.io :as io]))
|
[clojure.java.io :as io]))
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
; Finn mest regnfulle dag,
|
|
||||||
; lengste periode med regn
|
|
||||||
|
|
||||||
(ns weather-data.rainiestperiod
|
(ns weather-data.rainiestperiod
|
||||||
(:require [clojure.data.csv :as csv]
|
(:require [clojure.data.csv :as csv]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
|
@ -16,42 +13,37 @@
|
||||||
(try (Double. x)
|
(try (Double. x)
|
||||||
(catch Exception e 0)))
|
(catch Exception e 0)))
|
||||||
|
|
||||||
(defn rainLevel [entry] (convertToNumeric (nth entry 5)))
|
|
||||||
|
|
||||||
(defn location [entry] (nth entry 0))
|
|
||||||
|
|
||||||
(defn rainy [entry]
|
|
||||||
(if (> (rainLevel entry) 0) true false))
|
|
||||||
|
|
||||||
(defn parseTime [entry]
|
(defn parseTime [entry]
|
||||||
(let [custom-formatter (f/formatter "dd.MM.YYYY")]
|
(let [custom-formatter (f/formatter "dd.MM.YYYY")]
|
||||||
(f/parse custom-formatter (nth entry 1))))
|
(f/parse custom-formatter (nth entry 1))))
|
||||||
|
|
||||||
(defn consecutiveDay [entry1 entry2]
|
(defn consecutiveDay [entry1 entry2]
|
||||||
(or (= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2))
|
(or (empty? entry1)
|
||||||
(= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2))))
|
(= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2))
|
||||||
|
(= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2))))
|
||||||
|
|
||||||
(def listOfPlaces (partition-by (fn [entry] (nth entry 0)) (rest csv)))
|
(defn filterRain [place]
|
||||||
|
(letfn [(getRainLevel [entry] (convertToNumeric (nth entry 5)))]
|
||||||
|
(filter #(> (convertToNumeric (getRainLevel %)) 0) place)))
|
||||||
|
|
||||||
(def rolldal (nth listOfPlaces 0))
|
(def rainObservationsByPlace
|
||||||
|
(letfn [(location [entry] (nth entry 0))]
|
||||||
|
(partition-by location (filterRain (rest csv)))))
|
||||||
|
|
||||||
(defn rainyEntries [place] (filter #(> (convertToNumeric (rainLevel %)) 0) place))
|
(defn findRainyPeriods
|
||||||
|
"Builds nested list of rainy periods: [[[<-entry->]<-rainyperiods->]]"
|
||||||
|
([remaining] (findRainyPeriods [[(first remaining)]] (rest remaining)))
|
||||||
|
([result remaining]
|
||||||
|
(if(empty? remaining) result
|
||||||
|
(if(consecutiveDay (last (last result)) (first remaining))
|
||||||
|
(recur (conj (vec (butlast result)) (conj (last result) (first remaining))) (rest remaining))
|
||||||
|
(recur (conj result [(first remaining)]) (rest remaining))))))
|
||||||
|
|
||||||
(defn addToLastPeriod [x y]
|
(def rainyPeriods (map findRainyPeriods rainObservationsByPlace))
|
||||||
(vec (conj (butlast x) (vec (conj (last x) y)))))
|
|
||||||
|
|
||||||
(defn createNewPeriod [x y]
|
(defn findRainiestPeriod [periods]
|
||||||
(vec (conj x [y])))
|
(let [rainiestPeriods (map #(last (sort-by count %)) periods)]
|
||||||
|
(last (sort-by count rainiestPeriods))))
|
||||||
|
|
||||||
(defn rainyPeriods [raindata] (reduce
|
(findRainiestPeriod rainyPeriods)
|
||||||
(fn [x y]
|
|
||||||
(if(= [[[]]] x)
|
|
||||||
(addToLastPeriod (last (last x)) y)
|
|
||||||
(if(consecutiveDay (last (last x)) y)
|
|
||||||
(do (println "addingToLastperiod")
|
|
||||||
(addToLastPeriod x y))
|
|
||||||
(createNewPeriod x y))))
|
|
||||||
[[[]]]
|
|
||||||
raindata))
|
|
||||||
|
|
||||||
(rainyPeriods (take 5 (rainyEntries rolldal)))
|
|
||||||
|
|
|
@ -1,28 +1,12 @@
|
||||||
(ns weather-data.rainiestperiod-test
|
(ns weather-data.rainiestperiod-test
|
||||||
(:require [clojure.test :refer :all][weather-data.rainiestperiod :refer :all :as weather]))
|
(:require [clojure.test :refer :all][weather-data.rainiestperiod :refer :all :as weather]))
|
||||||
|
|
||||||
(deftest add-1-to-1
|
|
||||||
(is (= 2 (+ 1 1))))
|
|
||||||
|
|
||||||
(deftest testAddToLastPeriod
|
|
||||||
(let [x [[["entry1"]["entry2"]]]
|
|
||||||
y ["entry3"]
|
|
||||||
result [[["entry1"]["entry2"]["entry3"]]]]
|
|
||||||
(is (= (weather/addToLastPeriod x y) result))))
|
|
||||||
|
|
||||||
(deftest testCreateNewPeriod
|
|
||||||
(let [x [[["entry1"]["entry2"]]]
|
|
||||||
y ["entry3"]
|
|
||||||
result [[["entry1"]["entry2"]][["entry3"]]]]
|
|
||||||
(is (= (weather/createNewPeriod x y) result))))
|
|
||||||
|
|
||||||
(deftest testConsecutiveDay
|
(deftest testConsecutiveDay
|
||||||
(let [x [[["RØLDAL" "26.10.2014" "-" "-" "-" "42.7" "0" "0" "Regn"]]]
|
(let [x [[["RØLDAL" "26.10.2014" "-" "-" "-" "42.7" "0" "0" "Regn"]]]
|
||||||
y ["RØLDAL" "27.10.2014" "-" "-" "-" "57.0" "0" "0" "Regn"]]
|
y ["RØLDAL" "27.10.2014" "-" "-" "-" "57.0" "0" "0" "Regn"]]
|
||||||
(is (weather/consecutiveDay (last (last x)) y))))
|
(is (weather/consecutiveDay (last (last x)) y))))
|
||||||
|
|
||||||
|
(def initData (take 5 (first weather/rainObservationsByPlace)))
|
||||||
(def initData (take 5 (weather/rainyEntries weather/rolldal)))
|
|
||||||
(def result [[["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
(def result [[["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
||||||
["RØLDAL" "02.01.1960" "-" "-" "-" "2.0" "19" "4" "Regn"]]
|
["RØLDAL" "02.01.1960" "-" "-" "-" "2.0" "19" "4" "Regn"]]
|
||||||
[["RØLDAL" "05.01.1960" "-" "-" "-" "3.0" "15" "4" "Regn"]
|
[["RØLDAL" "05.01.1960" "-" "-" "-" "3.0" "15" "4" "Regn"]
|
||||||
|
@ -30,6 +14,6 @@
|
||||||
["RØLDAL" "07.01.1960" "-" "-" "-" "0.2" "15" "4" "Regn"]]])
|
["RØLDAL" "07.01.1960" "-" "-" "-" "0.2" "15" "4" "Regn"]]])
|
||||||
|
|
||||||
(deftest testRainyPeriods
|
(deftest testRainyPeriods
|
||||||
(is (= (weather/rainyPeriods initData) result)))
|
(is (= (weather/findRainyPeriods initData) result)))
|
||||||
|
|
||||||
(run-tests)
|
(run-tests)
|
||||||
|
|
Loading…
Reference in a new issue