Coder Social home page Coder Social logo

hsiaoping-zhang / reservoir-data-visualization Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 2.97 MB

將水庫查詢網頁的歷年資料爬下來並存成 csv 檔,並從中整理出 function 針對不同天數長度的資料提取,最後用 Plotly 呈現圖表

Python 58.81% Jupyter Notebook 41.19%
data-visualization plotly pandas crawler python reservoir

reservoir-data-visualization's Introduction

臺灣水庫歷年資料視覺化 Reservoir Data Visiualization

成果說明 PPT
Plotly 圖表呈現

網頁爬蟲

透過 crawler_function.pywrite_file.py 兩個檔案,可以將水庫資料網站上的每天資料爬蟲下來,寫成 csv 檔,讓以後讀取資料較為方便。

主程式為 : grab_data.py ,更多詳細說明在 : HaackMD筆記

crawler_function.py

FUNCTION :

  • find_value(name, web)
    Search for a certain value (ex: __VIEWSTATE)
    在 web 內容裡找到特定的 value 值

  • table_data_management(table, reservoir_dict_list, total_reservoir, index)
    Manage DataFrame, get values from assigned columns, and return them.
    處理DataFrame type的資料,並從 index list 當中獲得要存取的欄位,加到reservoir_dict_list當中 (由不同欄位資料的dictionary組成的list type),並回傳。

  • reservoir_data_search(date, reservoir_dict_list, total_reservoir, index)
    Get __EVENTTARGET 's and __VIEWSTATE 's value by using find_value function, and request to website using POST.
    先用 GET 的方式存取到兩個參數的值,用 POST 方式放入參數向網頁抓取資料。

write_file.py

FUNCTION :

  • write_csv_file(reservoir_dict_list, length, date_index, year)
    Write DataFrame to csv file.
    寫檔。

資料視覺化

把歷年的水庫水位資料從網站上取得後,有別於政府開放資料只提供即時資料,利用這份 project 可以透過將資料視覺化的過程,顯示單個或多個水庫在一段期間當中的水位變化,以及是否擁有季節性的水位規律。

fun_get_value.py

內有取得單日、每 n 日、每月、每年的 function 定義,可供 main 程式碼呼叫。

FUNCTION :

  • a_day_value(path, file_name, date, reservoir, string)
    Get one day's value with all type data from all reservoirs. ( if data is percentage : sting="percentage" )
    獲得單日資料,如果是百分比資料,則用 string = "percentage" 去掉 "%"

  • n_days_value(path, filename, year, reservoir_name, days, string)
    Read data and select every n(=days) days as a data point.
    讀入資料以每 n 天的平均值當作新的資料點,通常用於看整年每 n 天 (n < 31) 的資料趨勢

  • month_value(path, filename, year, reservoir, string)
    Get average month's value of assigned years.
    (if value is 5 times larger than average of past, print it out and skip adding it)
    分別讀取每年每個月的進水量平均資料,如果該月平均水量大於過去平均值 (判定為離群值) 5 倍,則不進行統計。

  • year_value(path, file, reservoir_name, year_list, compute, string)
    total or average waterflow of each year
    依據每年的總/平均水量計算

  • add_row_column(num, upper_bound)
    Add number (normal situation) or return to 1 (number is up to upper_bound)

  • period_list(days, year)
    Use days to compute date points of every period.
    根據 days (日期間隔) 和 year (年份),得到每段期間的日期點,作為作圖的 x 軸參考。

fun_ploting.py

畫出長條圖、折線圖或多圖呈現的 function 定義,可供 main 程式碼呼叫。

FUNCTION :

  • bar_plot(x, y, name, title, xaxis, yaxis, file_name) 長條圖

  • line_plot(x, y, name, title, xaxis, yaxis, file_name) 折線圖

  • sub_plot(x, y, name, title, xaxis, yaxis, file_name, row, column) 多圖呈現

main.py

有四個主要程式碼區快的展現,分別為:

  1. 單日的水庫有效蓄水量與總容量的長條圖比較
  2. 單個水庫多年的每 n 天平均蓄水量折線圖比較
  3. 多個水庫的平均進水量折線圖(在同一張圖) & 長條圖(多圖)比較
  4. 單個水庫整年進水總量與出水總量的多圖(長條圖)呈現

Block 1 單日 sample 成果

網頁呈現 website view - Plotly image

根據指定日期比較單日資料之間的差異(ex: 現在蓄水量 / 有效總蓄水量)

Block 2 每 n 日 sample 成果

網頁呈現 website view - Plotly image 曾文水庫多年的每 10 天平均蓄水量折線圖比較

Block 3 每個月 sample 成果

網頁呈現 website view - Plotly image
多個水庫的平均月進水量折線圖在同一張圖比較

每個月的 subplot 圖成果

網頁呈現 website view - Plotly image
多個水庫的平均月進水量長條圖多圖比較

Block 4 每年 sample 成果

網頁呈現 website view - Plotly image 曾文水庫整年進水總量與出水總量的兩圖(長條圖)呈現

reservoir-data-visualization's People

Contributors

hsiaoping-zhang avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.