iT邦幫忙

2024 iThome 鐵人賽

DAY 16
0
AI/ ML & Data

使用 jq 處理資料系列 第 16

Day16: 練習用 jq 追颱風消息

  • 分享至 

  • xImage
  •  

颱風來了,在做好防颱準備之後,我們用颱風警報的資訊做練習吧!

❗如果有信心,可以先不要往下看,自己嘗試一下 👉

觀察資料

透過資料來源的說明,可以知道取得的資料裡面會有 analysis_data: 過去及現在熱帶氣旋位置。

  • STEP1: 取得 json 資料,本地練習使用
curl -s https://opendata.cwa.gov.tw/api/v1/rest/datastore/W-C0034-005?Authorization=${你的授權碼} > typhoon0930.json
  • STEP2: 使用 keys 看看資料
jq "keys" typhoon0930.json

https://ithelp.ithome.com.tw/upload/images/20240930/200783892ModEqTDSZ.png

  • STEP3: 往下找好幾層,終於看到 analysis_data

https://ithelp.ithome.com.tw/upload/images/20240930/20078389pzjcYRQ4Gf.png

  • STEP4: 似乎有兩個 analysis_data,透過名字來看看該看哪一個吧。
jq ".records.tropicalCyclones.tropicalCyclone[] | {typhoonName, cwaTyphoonName}" typhoon0930
.json

https://ithelp.ithome.com.tw/upload/images/20240930/20078389nI1H8uXuzi.png

篩選資料、一邊篩選一邊觀察

  • STEP5: 鎖定 山陀兒 颱風 select(.typhoonName==\"KRATHON\"),底下的 .analysisData.fix 陣列裡面,目前有 34 筆資料。
jq ".records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix | length" typhoon0930.json

34

  • STEP6: .analysisData.fix 陣列裡面一個元素有以下屬性名稱
jq -c ".records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix[0] | keys_unsorted" typhoon0930.json

["fixTime","coordinate","maxWindSpeed","maxGustSpeed","pressure","movingSpeed","movingDirection","movingPrediction"]

  • STEP7: 觀察最後一個元素
jq -c ".records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix[-1] " typhoon0930.json
{"fixTime":"2024-09-30T20:00:00+08:00","coordinate":"120.5,20.5","maxWindSpeed":"48","maxGustSpeed":"58","pressure":"930","movingSpeed":"10","movingDirection":"W","movingPrediction":[{"value":"以每小時6公里速度,向西北西轉北進行","lang":"zh-hant"},{"value":"WNW TURNING NORTH 6 KM/HR","lang":"en-us"}],"circleOf15Ms":{"radius":"200","quadrantRadii":{"radius":[{"value":"190","dir":"NE"},{"value":"200","dir":"SE"},{"value":"200","dir":"SW"},{"value":"200","dir":"NW"}]}},"circleOf25Ms":{"radius":"70","quadrantRadii":{"radius":[{"value":"190","dir":"NE"},{"value":"200","dir":"SE"},{"value":"200","dir":"SW"},{"value":"200","dir":"NW"}]}}}

選擇要看的資料屬性

  • STEP8: 選擇要看的資料屬性,每一筆資料要用陣列呈現
jq -c "[.records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix[-1] | .fixTime, .movingDirection, .coordinate, .pressure, .maxWindSpeed, .maxGustSpeed, .moving
Prediction[0].value] " typhoon0930.json

["2024-09-30T20:00:00+08:00","W","120.5,20.5","930","48","58","以每小時6公里速度,向西北西轉北進行"]

  • STEP9: 取得所有內容
jq -c "[.records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix[] | [.fixTime, .movingDirection, .coordinate, .pressure, .maxWindSpeed, .maxGustSpeed, .movingPrediction[0].value]][] " typhoon0930.json
["2024-09-30T02:00:00+08:00","WNW","122.5,19.8","945","43","53",null]
["2024-09-30T03:00:00+08:00","W","122.4,19.8","945","43","53",null]
["2024-09-30T04:00:00+08:00","N","122.4,19.9","945","43","53",null]
["2024-09-30T05:00:00+08:00","W","122.3,19.9","945","43","53",null]
["2024-09-30T06:00:00+08:00","W","122.2,19.9","945","43","53",null]
["2024-09-30T07:00:00+08:00","N","122.2,20.0","945","43","53",null]
["2024-09-30T08:00:00+08:00","W","122.1,20.0","940","45","55",null]
["2024-09-30T09:00:00+08:00","NNW","122.0,20.2","940","45","55",null]
["2024-09-30T10:00:00+08:00","W","121.9,20.2","940","45","55",null]
["2024-09-30T11:00:00+08:00","W","121.8,20.2","940","45","55",null]
["2024-09-30T12:00:00+08:00","NW","121.6,20.4","940","45","55",null]
["2024-09-30T13:00:00+08:00","W","121.5,20.4","940","45","55",null]
["2024-09-30T14:00:00+08:00","NW","121.4,20.5","940","45","55",null]
["2024-09-30T15:00:00+08:00","W","121.2,20.5","940","45","55",null]
["2024-09-30T16:00:00+08:00","W","121.0,20.5","940","45","55",null]
["2024-09-30T17:00:00+08:00","W","120.8,20.5","940","45","55",null]
["2024-09-30T18:00:00+08:00","W","120.7,20.5","940","45","55",null]
["2024-09-30T19:00:00+08:00","W","120.6,20.5","940","45","55",null]
["2024-09-30T20:00:00+08:00","W","120.5,20.5","930","48","58","以每小時6公里速度,向西北西轉北進行"]

結論

今天練習到這邊,已經拿到足夠追蹤颱風消息的資料了~ 不知道認真學習的各位,有沒有順利拿到想看的資料了呢?有沒有信心? 😊 有沒有成就感? 😊

其他呈現和輸出部分,明天後天再繼續學習吧! 做好防颱準備~ GO !


上一篇
Day15: keys 和 keys_unsorted
下一篇
Day17: 練習用 jq 追颱風消息 - 字串 Slice 和 Split
系列文
使用 jq 處理資料30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
lindasharry
iT邦新手 5 級 ‧ 2024-10-30 16:44:07

10月底竟然有颱風,真是罕見。讓我們來看看這個颱風的詳細資料吧。

1. 用以下指令認識颱風的名字

其中 AUTHORIZATION 要使用自己註冊後取得的授權碼

set AUTHORIZATION=自己註冊後取得的授權碼

curl -s https://opendata.cwa.gov.tw/api/v1/rest/datastore/W-C0034-005?Authorization=%AUTHORIZATION% | jq ".records.tropicalCyclones.tropicalCyclone[] | {typhoonName, cwaTyphoonName}"

執行結果

{
  "typhoonName": "KONG-REY",
  "cwaTyphoonName": "康芮"
}

2. 下載 json

curl -s https://opendata.cwa.gov.tw/api/v1/rest/datastore/W-C0034-005?Authorization=%AUTHORIZATION% | jq > typhoon1030.json

3. 使用先前的程式,將颱風名稱改為 KONG-REY

jq -c -r -s --arg tname KONG-REY -f main.jq direction.json typhoon1030.json beaufort_wind.json

執行結果

時間            經度    緯度    氣壓    風速MAX         陣風MAX         方向    預測
2024-10-30T12   124.4   19.5    915     16 颶風++++     爆表了(°Д°)     西北 ↖
2024-10-30T13   124.3   19.6    915     16 颶風++++     爆表了(°Д°)     西北 ↖
2024-10-30T14   124.2   19.7    915     16 颶風++++     爆表了(°Д°)     西北 ↖
2024-10-30T15   124.2   19.9    915     16 颶風++++     爆表了(°Д°)     北 ↑
2024-10-30T16   124.0   20.0    915     16 颶風++++     爆表了(°Д°)     WNW     以每小時20公里速度,向西北轉北北西進行

我要留言

立即登入留言