2024年1月6日 星期六

Node-Red 擷取氣象資料開放平台 36小時天氣預報

 一般天氣預報-今明36小時天氣預報

參考來源 https://steam.oxxostudio.tw/category/python/spider/forecast.html











locationName: "臺北市"

weatherElement: array[5]

weatherElement 裡有五種的預報因子,可透過程式單純取出所需的預報因子。

預報因子說明
Wx天氣現象
MaxT最高溫度
MinT最低溫度
CI舒適度
PoP降雨機率


0: object

elementName: "Wx"

time: array[3]

0: object

startTime: "2024-01-06T18:00:00+08:00"

endTime: "2024-01-07T06:00:00+08:00"

parameter: object

parameterName: "多雲"

parameterValue: "4"

1: object

startTime: "2024-01-07T06:00:00+08:00"

endTime: "2024-01-07T18:00:00+08:00"

parameter: object

parameterName: "陰天"

parameterValue: "7"

2: object

startTime: "2024-01-07T18:00:00+08:00"

endTime: "2024-01-08T06:00:00+08:00"

parameter: object

parameterName: "陰天"

parameterValue: "7"


elementName: "MaxT"

time: array[3]

0: object

startTime: "2024-01-06T18:00:00+08:00"

endTime: "2024-01-07T06:00:00+08:00"

parameter: object

parameterName: "19"

parameterUnit: "C"

1: object

startTime: "2024-01-07T06:00:00+08:00"

endTime: "2024-01-07T18:00:00+08:00"

parameter: object

parameterName: "18"

parameterUnit: "C"


2: object

startTime: "2024-01-07T18:00:00+08:00"

endTime: "2024-01-08T06:00:00+08:00"

parameter: object

parameterName: "16"

parameterUnit: "C"

elementName: "MinT"



time: array[3]

3: object

elementName: "CI"

time: array[3]


4: object

elementName: "PoP"

time: array[3]


=====================================

var rawdata =context.global.weather_records;
var citynumber=rawdata.length;
var site_name=msg.payload;

var i;
var Temp1,Temp2,Temp3,Temp4;


for (i=0; i<citynumber;i++)

{

    if (rawdata[i].locationName==site_name)

    {

        Temp1=rawdata[i].weatherElement[0].time[0].parameter.parameterName;

        Temp2=rawdata[i].weatherElement[0].time[0].startTime;

        Temp3=rawdata[i].weatherElement[0].time[0].endTime;

        Temp4= site_name + Temp1+ '--'+ '開始時間: '+ Temp2+ '結束時間: '+ Temp3;

        break;

    }

}

msg.payload= Temp4;

return msg;
==============================================
var rawdata =context.global.weather_records

var citynumber=rawdata.length;

var i;
var Temp1,Temp2,Temp3,Temp4,Temp5,Temp6;

var weather=[];

for (i=0; i<citynumber;i++)

{
    Temp1=rawdata[i].locationName;
    Temp2=rawdata[i].weatherElement[0].time[0].parameter.parameterName;
    Temp3=rawdata[i].weatherElement[1].time[0].parameter.parameterName;
    Temp4=rawdata[i].weatherElement[2].time[0].parameter.parameterName;   
    Temp5=rawdata[i].weatherElement[3].time[0].parameter.parameterName;
    Temp6=rawdata[i].weatherElement[4].time[0].parameter.parameterName;  
    weather[i]=[Temp1,Temp2,Temp3,Temp4,Temp5,Temp6];
}

msg.payload=weather  ;

return msg;

//Temp1=rawdata[i].weatherElement[0].time[0].parameter.parameterName;

===================================================
[{"id":"468f922801fa005a","type":"http request","z":"5b0443f33d97ce45","name":"今明36小時天氣預報","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://opendata.cwa.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=CWB-40C25FFF-1224-4250-B9D9-3735AAE17DBF&downloadType=WEB&format=JSON","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":300,"y":120,"wires":[["254bac363b9f06a0","9f6d9a3d8fbc7a82","f48eec28efb890b5"]]},{"id":"254bac363b9f06a0","type":"debug","z":"5b0443f33d97ce45","name":"今明36小時天氣預報 message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":60,"wires":[]},{"id":"9f6d9a3d8fbc7a82","type":"function","z":"5b0443f33d97ce45","name":"今明36小時天氣預報 data process","func":"msg.payload=msg.payload.cwaopendata.dataset.location;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":140,"wires":[["288f6994b4ded07b","b107f043e23d2bd7"]]},{"id":"288f6994b4ded07b","type":"debug","z":"5b0443f33d97ce45","name":"UVI process message","active":true,"console":"false","complete":"payload","x":900,"y":140,"wires":[]},{"id":"f48eec28efb890b5","type":"function","z":"5b0443f33d97ce45","name":"Set context.global.36weather","func":"context.global.weather_records=msg.payload.cwaopendata.dataset.location;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":100,"wires":[[]]},{"id":"8ad8c198e4db7261","type":"ui_table","z":"5b0443f33d97ce45","group":"38f8f1c1d386a910","name":"未來8小時天氣預報","order":5,"width":10,"height":12,"columns":[],"outputs":0,"cts":false,"x":1030,"y":220,"wires":[]},{"id":"a2d89722898228ba","type":"ui_button","z":"5b0443f33d97ce45","name":"","group":"38f8f1c1d386a910","order":1,"width":4,"height":1,"passthru":false,"label":"取得未來8小時天氣預報","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":130,"y":180,"wires":[["468f922801fa005a"]]},{"id":"b107f043e23d2bd7","type":"function","z":"5b0443f33d97ce45","name":"get All Sitename","func":"var rawdata =msg.payload;\nvar site_number=rawdata.length;\nvar site_array=[];\nvar i;\n\nfor (i=0; i<site_number;i++)\n{\n    site_array[i]=rawdata[i].locationName;\n}\n\nmsg.payload=site_array;\n \nflow.set(\"weather_sitename\",site_array);\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":200,"wires":[["67b3b04db85bd20f","e8c469f411a6e967"]]},{"id":"67b3b04db85bd20f","type":"debug","z":"5b0443f33d97ce45","name":"debug 248","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":200,"wires":[]},{"id":"e8c469f411a6e967","type":"change","z":"5b0443f33d97ce45","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":240,"wires":[["188de9a5f3485a9e"]]},{"id":"188de9a5f3485a9e","type":"ui_dropdown","z":"5b0443f33d97ce45","name":"","label":"今明36小時天氣預報","tooltip":"","place":"Select option","group":"38f8f1c1d386a910","order":3,"width":4,"height":1,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":540,"y":300,"wires":[["c6b7947c063a1699","8aece592f0df5fb1","dae0694d329b08cf"]]},{"id":"8aece592f0df5fb1","type":"debug","z":"5b0443f33d97ce45","name":"debug 249","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":340,"wires":[]},{"id":"1f7c9728f01d9286","type":"debug","z":"5b0443f33d97ce45","name":"debug  ","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":360,"wires":[]},{"id":"c6b7947c063a1699","type":"function","z":"5b0443f33d97ce45","name":"get 站名 今明36小時天氣預報","func":"var rawdata =context.global.weather_records;\nvar citynumber=rawdata.length;\nvar site_name=msg.payload;\n\nvar i;\nvar Temp1,Temp2,Temp3,Temp4;\n\n\nfor (i=0; i<citynumber;i++)\n\n{\n\n    if (rawdata[i].locationName==site_name)\n\n    {\n\n        Temp1=rawdata[i].weatherElement[0].time[0].parameter.parameterName;\n\n        Temp2=rawdata[i].weatherElement[0].time[0].startTime;\n\n        Temp3=rawdata[i].weatherElement[0].time[0].endTime;\n\n        Temp4= site_name + Temp1+ '--'+ '開始時間: '+ Temp2+ '結束時間: '+ Temp3;\n\n        break;\n\n    }\n\n}\n\nmsg.payload= Temp4;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":380,"wires":[["1f7c9728f01d9286","aee4a05fc573b83e"]]},{"id":"94908a98162d72ef","type":"inject","z":"5b0443f33d97ce45","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":100,"y":120,"wires":[["468f922801fa005a"]]},{"id":"dae0694d329b08cf","type":"function","z":"5b0443f33d97ce45","name":"get  Sitename  all records","func":"var rawdata =context.global.weather_records\n\nvar citynumber=rawdata.length;\n\nvar i;\nvar Temp1,Temp2,Temp3,Temp4,Temp5,Temp6;\n\nvar weather=[];\n\nfor (i=0; i<citynumber;i++)\n\n{\n    Temp1=rawdata[i].locationName;\n    Temp2=rawdata[i].weatherElement[0].time[0].parameter.parameterName;\n    Temp3=rawdata[i].weatherElement[1].time[0].parameter.parameterName;\n    Temp4=rawdata[i].weatherElement[2].time[0].parameter.parameterName;   \n    Temp5=rawdata[i].weatherElement[3].time[0].parameter.parameterName;\n    Temp6=rawdata[i].weatherElement[4].time[0].parameter.parameterName;  \n    weather[i]=[Temp1,Temp2,Temp3,Temp4,Temp5,Temp6];\n}\n\nmsg.payload=weather  ;\n\nreturn msg;\n\n//Temp1=rawdata[i].weatherElement[0].time[0].parameter.parameterName;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":260,"wires":[["8ad8c198e4db7261","34cdb065112c0a2a"]]},{"id":"34cdb065112c0a2a","type":"debug","z":"5b0443f33d97ce45","name":"debug 250","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":300,"wires":[]},{"id":"2d6065e985a20843","type":"comment","z":"5b0443f33d97ce45","name":"一般天氣預報-今明36小時天氣預報","info":"一般天氣預報-今明36小時天氣預報","x":180,"y":40,"wires":[]},{"id":"aee4a05fc573b83e","type":"ui_text","z":"5b0443f33d97ce45","group":"38f8f1c1d386a910","order":4,"width":10,"height":1,"name":"","label":"未來8小時天氣","format":"{{msg.payload}}","layout":"row-spread","className":"","x":1060,"y":400,"wires":[]},{"id":"38f8f1c1d386a910","type":"ui_group","name":"Default","tab":"6571c5231932db28","order":2,"disp":true,"width":10,"collapse":false,"className":""},{"id":"6571c5231932db28","type":"ui_tab","name":"2024一般天氣預報","icon":"dashboard","disabled":false,"hidden":false}]

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...