颱風來了,在做好防颱準備之後,我們用颱風警報的資訊做練習吧!
下圖是預計要的結果:
資料的來源是 https://opendata.cwa.gov.tw/ 的 颱風消息與警報-熱帶氣旋路徑,使用資料會需要註冊,並取得授權碼喔!
❗如果有信心,可以先不要往下看,自己嘗試一下 👉 |
---|
透過資料來源的說明,可以知道取得的資料裡面會有 analysis_data
: 過去及現在熱帶氣旋位置。
curl -s https://opendata.cwa.gov.tw/api/v1/rest/datastore/W-C0034-005?Authorization=${你的授權碼} > typhoon0930.json
jq "keys" typhoon0930.json
analysis_data
analysis_data
,透過名字來看看該看哪一個吧。jq ".records.tropicalCyclones.tropicalCyclone[] | {typhoonName, cwaTyphoonName}" typhoon0930
.json
select(.typhoonName==\"KRATHON\")
,底下的 .analysisData.fix
陣列裡面,目前有 34 筆資料。jq ".records.tropicalCyclones.tropicalCyclone[] | select(.typhoonName==\"KRATHON\") | .analysisData.fix | length" typhoon0930.json
34
.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"]
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"}]}}}
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公里速度,向西北西轉北進行"]
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 !
10月底竟然有颱風,真是罕見。讓我們來看看這個颱風的詳細資料吧。
其中 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": "康芮"
}
curl -s https://opendata.cwa.gov.tw/api/v1/rest/datastore/W-C0034-005?Authorization=%AUTHORIZATION% | jq > typhoon1030.json
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公里速度,向西北轉北北西進行