0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

気象庁予報の 天気コード(テロップ番号) を PHP で 絵文字に変換してみる

Posted at

さらにティッカーを短く表示させるために

気象庁予報の JSON ファイルを PHP で RSS 形式に変換してみるでは、気象庁予報の JSON ファイルのうち、予報文を使ってティッカー化した。
しかし、他の情報も合わせて表示させる際には長く感じられるため、同じく JSON ファイルに含まれる天気コード(テロップ番号とも呼ばれる)を使って天気を絵文字化することにした。
気象庁予報の JSON ファイルを Node-Red で Awtrix 用に変換してみる と合わせて見ていただきたい。

対応表を CSV ファイルにする

予めテロップ番号と絵文字の対応表を作っておかないといけない。
公開されているテロップ番号と天気マークの対応表を参考にして

まずはLibreOfficeで縦方向に対応表を作成。
telopicon_1.png

次に、別のシートに 行/列を入れ替えて貼り付け で対応表を横方向にする。
念のためテロップ番号の書式(分類)をテキストにしておく。
telopicon_2.png

CSV に書き出し。文字コードは UTF-8 、すべてのセルを引用符で囲むにチェック。
telopicon_3.png

PHP

対応表を横方向に作ったわけは、 PHP の fgetcsv 関数が1行ずつ読み込むタイプだからである。
fgetcsv を逐次実行すると、1回目は1行目、2回目は2行目を読み込むことになり、それぞれ $code$icon というように1行丸ごと配列に入れることができる。
あとは str_replace の置換前・置換後にそれぞれの配列を指定すればよい。

さらに表示を簡略化するため、

  • 当日の天気には降水確率のみ(予報時刻に近い方から2つ分=12時間分)
  • 翌日の天気には翌日の予想最低最高気温のみ

を表示させる。

予想気温のデータは4つ(5時予報と11時予報)の時と2つ(17時予報)の時があるが、どちらも後ろ2つが翌日の最低最高気温となっているため、取り出す際に後ろ2つを指定すればよい。

PHP で配列の最後から取り出す方法に end 関数を使う方法があるため、最初はそれを使うことを考えたが、後ろから2つ目を取り出す際には使えないことから、配列要素数を count で数え、その値を使って指定することにした。

以下が PHP スクリプトと実行例である。

jma_weathericon_rss_gen.php
<?php
//気象庁のAPIにて気象情報取得
$fp = fopen("../data/weathercode.csv", "r");

$code = fgetcsv($fp, escape:'');
$icon = fgetcsv($fp, escape:'');

fclose($fp);

$url = "https://www.jma.go.jp/bosai/forecast/data/forecast/200000.json";

$weather_json = file_get_contents($url);
$weather_array = json_decode($weather_json, true);

$date = $weather_array["0"]["timeSeries"]["0"]["timeDefines"]["0"];
$day = substr($date, 8, 2);
$hour = substr($date, 11, 2);

$jma_wc_1_1 = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weatherCodes"]["0"];
$jma_wc_1_2 = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weatherCodes"]["1"];
$jma_wc_2_1 = $weather_array["0"]["timeSeries"]["0"]["areas"]["1"]["weatherCodes"]["0"];
$jma_wc_2_2 = $weather_array["0"]["timeSeries"]["0"]["areas"]["1"]["weatherCodes"]["1"];


$jma_icon_1_1 = str_replace($code, $icon, $jma_wc_1_1);
$jma_icon_1_2 = str_replace($code, $icon, $jma_wc_1_2);
$jma_icon_2_1 = str_replace($code, $icon, $jma_wc_2_1);
$jma_icon_2_2 = str_replace($code, $icon, $jma_wc_2_2);

$jma_pop_1 = $weather_array["0"]["timeSeries"]["1"]["areas"]["0"]["pops"];
$jma_pop_2 = $weather_array["0"]["timeSeries"]["1"]["areas"]["1"]["pops"];

$jma_temp_1 = $weather_array["0"]["timeSeries"]["2"]["areas"]["0"]["temps"];
$jma_temp_2 = $weather_array["0"]["timeSeries"]["2"]["areas"]["1"]["temps"];

$jma_temp_c1 = count($jma_temp_1);
$jma_temp_c2 = count($jma_temp_2);
$jma_temp_1_1 = $jma_temp_1[$jma_temp_c1 - 2];
$jma_temp_1_2 = $jma_temp_1[$jma_temp_c1 - 1];
$jma_temp_2_1 = $jma_temp_2[$jma_temp_c2 - 2];
$jma_temp_2_2 = $jma_temp_2[$jma_temp_c2 - 1];

$fp = fopen("../rss/jma_weather.xml", "w");
fwrite($fp, "\xEF\xBB\xBF");
fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>気象庁</title>
<link>https://www.jma.go.jp/</link>
<description>長野県の天気</description>
<lastBuildDate>'.gmdate("D, d M Y H:i:s T").'</lastBuildDate>
<item><title>天気予報</title><description>'. $day . '日' . $hour . '時</description></item>
<item><title>【長野】今日</title><description>' . $jma_icon_1_1. ' ' . $jma_pop_1[0]. '% ' . $jma_pop_1[1] . '%</description></item>
<item><title>明日</title><description>' . $jma_icon_1_2. ' ' . $jma_temp_1_1. '/' . $jma_temp_1_2 .'℃</description></item>
<item><title>【松本】今日</title><description>' . $jma_icon_2_1. ' ' . $jma_pop_2[0]. '% ' . $jma_pop_2[1] . '%</description></item>
<item><title>明日</title><description>' . $jma_icon_2_2. ' ' . $jma_temp_2_1. '/' . $jma_temp_2_2 .'℃</description></item>
</channel></rss>
');
fclose($fp);
echo 'JMA RSS file generated.';

telopicon_4.jpg

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?