Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

weather

weather.livemd

weather

Mix.install([
  {:kino, "~> 0.9"},
  {:jason, "~> 1.4"},
  {:kino_vega_lite, "~> 0.1"}
])

Data

data =
  File.read!("F:/Git/WeatherStats/data/bremen_stats.json")
  |> Jason.decode!(keys: :atoms)
# data_text = Kino.Input.textarea("weather data")
# data = Jason.decode!(Kino.Input.read(data_text), keys: :atoms)

Filter

month = Kino.Input.text("month")
m = Kino.Input.read(month)

res =
  data
  |> Enum.filter(fn month ->
    month.date |> String.ends_with?(m)
  end)
  |> Enum.map(fn month ->
    %{year: month.date |> String.split("-") |> hd(), temp: month.avg_temp}
  end)

Show

Kino.DataTable.new(res)
VegaLite.new(width: 800, height: 400)
|> VegaLite.data_from_values(res, only: ["year", "temp"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "year", type: :temporal)
|> VegaLite.encode_field(:y, "temp", type: :quantitative)

Day

res =
  data =
  File.read!("F:/Git/WeatherStats/data/bremen.json")
  |> Jason.decode!(keys: :atoms)
  |> Enum.filter(fn entry ->
    entry.date
    |> String.ends_with?(
      "#{String.pad_leading(DateTime.utc_now().month |> Integer.to_string(), 2, "0")}-#{String.pad_leading(DateTime.utc_now().day |> Integer.to_string(), 2, "0")}"
    )
  end)
  |> Enum.map(fn day ->
    %{year: day.date |> String.split("-") |> hd(), temp: day.avg_temp}
  end)

res
|> Enum.min_max()
Kino.DataTable.new(res)
VegaLite.new(width: 800, height: 400)
|> VegaLite.data_from_values(res, only: ["year", "temp"])
|> VegaLite.mark(:point)
|> VegaLite.encode_field(:x, "year", type: :temporal)
|> VegaLite.encode_field(:y, "temp", type: :quantitative)
data =
  File.read!("F:/Git/WeatherStats/data/bremen.json")
  |> Jason.decode!(keys: :atoms)

res =
  1..Calendar.ISO.days_in_month(2022, DateTime.utc_now().month)
  |> Enum.map(fn day ->
    day_data =
      data
      |> Enum.filter(fn entry ->
        entry.date
        |> String.match?(
          ~r/20\d\d-#{String.pad_leading("#{DateTime.utc_now().month}", 2, "0")}-#{String.pad_leading("#{day}", 2, "0")}/
        )
      end)

    avg_temp =
      (Enum.reduce(day_data, 0, fn entry, acc -> acc + entry.avg_temp end) / length(day_data))
      |> Float.round(1)

    %{day: day, temp: avg_temp}
  end)
VegaLite.new(width: 800, height: 400)
|> VegaLite.data_from_values(res, only: ["day", "temp"])
|> VegaLite.mark(:bar)
|> VegaLite.encode_field(:x, "day", type: :quantitative)
|> VegaLite.encode_field(:y, "temp", type: :quantitative)

Month running

data =
  File.read!("F:/Git/WeatherStats/data/bremen.json")
  |> Jason.decode!(keys: :atoms)

days =
  1..DateTime.utc_now().day
  |> Enum.map(&String.pad_leading("#{&1}", 2, "0"))
  |> Enum.join("|")

days_data =
  data
  |> Enum.filter(fn entry ->
    entry.date
    |> String.match?(
      ~r/20\d\d-#{String.pad_leading("#{DateTime.utc_now().month}", 2, "0")}-(#{days})/
    )
  end)

(Enum.reduce(days_data, 0, fn day, acc ->
   acc + day.avg_temp
 end) / length(days_data))
|> Float.round(1)
hb =
  File.read!("../WeatherStats/data/bremen.json")
  |> Jason.decode!(keys: :atoms)
  |> Enum.filter(fn entry ->
    entry.date
    |> String.starts_with?("2024-12")
  end)
  |> Enum.map(&%{day: &1.date |> String.split("-") |> List.last(), sun: &1.sun, loc: "hb"})

sw =
  File.read!("../WeatherStats/data/schweinfurt.json")
  |> Jason.decode!(keys: :atoms)
  |> Enum.filter(fn entry ->
    entry.date
    |> String.starts_with?("2024-12")
  end)
  |> Enum.map(&%{day: &1.date |> String.split("-") |> List.last(), sun: &1.sun, loc: "sw"})

sun_data = hb ++ sw
VegaLite.new(widht: [step: 31])
|> VegaLite.data_from_values(sun_data, only: ["day", "sun", "loc"])
|> VegaLite.mark(:bar)
|> VegaLite.encode_field(:column, "day", type: :ordinal, spacing: 1)
|> VegaLite.encode_field(:y, "sun", type: :quantitative)
|> VegaLite.encode_field(:x, "loc", title: nil)
|> VegaLite.encode_field(:color, "loc")