nasaで遊んでみよう 3/8~3/11までの衛星写真

<?php

// EPIC
$API_KEY = "";
$date = array(
	"2018-03-06","2018-03-07","2018-03-08","2018-03-09","2018-03-10","2018-03-11"
);

foreach($date as $value){
	$date_url = str_replace("-", "/", $value);
	$BASE_URL = "https://api.nasa.gov/EPIC/api/natural/date/".$value."?api_key=".$API_KEY."";
	$obj = json_decode(file_get_contents($BASE_URL), true);
	$img = $obj&#91;2&#93;&#91;"image"&#93;;
	$img_URL = "https://epic.gsfc.nasa.gov/archive/natural/".$date_url."/jpg/".$img.".jpg";
	echo "<img src=\"".$img_URL."\" width=\"200\" >";
}

foreach($date as $value){
	$BASE_URL = "https://api.nasa.gov/EPIC/api/natural/date/".$value."?api_key=".$API_KEY."";
	$obj = json_decode(file_get_contents($BASE_URL), true);
	echo $obj[2]["date"] . "<br>";
}
?>

毎日$obj[2][“image”]でとると、5日に1回くらいの頻度でインド洋・アフリカ大陸の衛星写真になってしまいます。

データを増やしてます。

$date = array(
	"2018-03-01","2018-03-02","2018-03-03","2018-03-04","2018-03-05","2018-03-06","2018-03-07","2018-03-08","2018-03-09","2018-03-10","2018-03-11"
);

foreach($date as $value){
	$date_url = str_replace("-", "/", $value);
	$BASE_URL = "https://api.nasa.gov/EPIC/api/natural/date/".$value."?api_key=".$API_KEY."";
	$obj = json_decode(file_get_contents($BASE_URL), true);
	if($obj != null){
		$img = $obj[2]["image"];
		$img_URL = "https://epic.gsfc.nasa.gov/archive/natural/".$date_url."/jpg/".$img.".jpg";
		echo "<img src=\"".$img_URL."\" width=\"200\" >";
	}
}

大体インドネシアあたりが中心です。

そして、肝心のdiscovrですが、「地球から太陽方向に約150万km離れた太陽-地球ラグランジュ点L1をリサジュー軌道を描きながら周回する。」とのこと。

そのリサジュー軌道とは、物体が推進力なしで三体のラグランジュ点の回りを周回することのできる擬軌道。二体の秤動点の周りのリアプノフ軌道が、二体のなす平面に完全に含まれるのに対し、リサジュー軌道はそれに垂直な平面に含まれる成分を持ち、リサジュー図形を描く。

リサジュー図形

ラグランジュ点
天体力学における円制限三体問題の5つの平衡解

円制限三体問題
三体問題とは、重力ポテンシャルの下、相互作用する三質点系の運動の問題。天体力学では万有引力により相互作用する天体の運行をモデル化した問題

推進力なしで回ってるから、毎日の写真の経度がずれるって認識でOKかな?

Nasaで遊んでみよう EPICの写真を取得

DSCOVR’s Earth Polychromatic Imaging Camera (EPIC)

nasaの画像のURLがyyyyy/mm/ddとなっているため、str_replaceで置換します。

<?php

// EPIC
$API_KEY = "hogehoge";
$date = "2018-03-10"; // YYYY-MM-DD
$date_url = str_replace("-", "/", $date);

$BASE_URL = "https://api.nasa.gov/EPIC/api/natural/date/".$date."?api_key=".$API_KEY."";


$obj = json_decode(file_get_contents($BASE_URL), true);


foreach($obj as $value){
	$img = $value&#91;"image"&#93;;
	$img_URL = "https://epic.gsfc.nasa.gov/archive/natural/".$date_url."/jpg/".$img.".jpg";
	echo "<img src=\"".$img_URL."\" width=\"200\" >";
}

foreach($obj as $value){
	 echo $value["date"]. "<br>";
}
?>

dataを見ると、だいたい108分3秒(6483秒)に1回ごと、写真を取っているようですね。
60*60*24 / 6483 = 13.32なので、毎日、経度の位置がずれます。

2018-03-11 00:31:45
2018-03-11 04:07:50
2018-03-11 05:55:53
2018-03-11 07:43:56
2018-03-11 09:31:58
2018-03-11 11:20:01
2018-03-11 13:08:04
2018-03-11 14:56:07
2018-03-11 16:44:10
2018-03-11 18:32:13
2018-03-11 20:20:16

2018-03-10 00:50:26
2018-03-10 02:38:29
2018-03-10 04:26:33
2018-03-10 06:14:35
2018-03-10 08:02:38
2018-03-10 09:50:41
2018-03-10 11:38:44
2018-03-10 13:26:47
2018-03-10 15:14:50
2018-03-10 17:02:53
2018-03-10 18:50:56
2018-03-10 20:38:59
2018-03-10 22:27:02

2018-03-09 00:59:48
2018-03-09 02:47:51
2018-03-09 04:35:54
2018-03-09 06:23:56
2018-03-09 08:11:59
2018-03-09 10:00:02
2018-03-09 11:48:05
2018-03-09 13:36:08
2018-03-09 15:24:11
2018-03-09 17:12:14

Nasaで遊んでみよう Asteroids

Jet Propulsion Laboratory: JPLとは、Nasaのジェット推進研究所@カリフォルニア州パサデナ
Orbital Elements は軌道要素 

<?php

// Asteroids
$API_KEY = "";
$start_date = "2018-03-01"; // YYYY-MM-DD
$end_date = "2018-03-05"; // YYYY-MM-DD
$BASE_URL = "https://api.nasa.gov/neo/rest/v1/feed?start_date=".$start_date."&end_date=".$end_date."&api_key=".$API_KEY."";


$obj = json_decode(file_get_contents($BASE_URL), true);

print_r("<pre>");
var_dump($obj);
print_r("</pre>");
?>

全部数値化できるんですね。なるほど。

Nasaで遊んでみよう Astronomy Pic

<?php

// APOD
$API_KEY = "";
$Date = ""; // YYYY-MM-DD
$BASE_URL = "https://api.nasa.gov/planetary/apod?api_key=".$API_KEY."";

$obj = json_decode(file_get_contents($BASE_URL), true);

// print_r("<pre>");
// var_dump($obj);
// print_r("</pre>");

$img = $obj['hdurl'];
echo "<img src=\"".$img."\" width=\"200\" >";
?>

これは理解を超えてます。over head

Elementのmodule switch文に雪を追加

<?php 

function convert($main){
	switch($main){
			case 'Clear':
				return "晴れ<img src=\"img/icon01.png\">";
				break;
			case 'Clouds':
				return "曇<img src=\"img/icon02.png\">";
				break;
			case 'Rain':
				return "雨<img src=\"img/icon03.png\">";
				break;
			case 'Snow':
				return "雪<img src=\"img/icon08.png\">";
				break;
			default:
				echo $main;
		}
}

function convert2($description){
	switch($description){
			case 'clear sky':
				return "晴天<br>";
				break;
			case 'scattered clouds':
				return "きれぎれに浮かんでいる雲";
				break;
			case 'broken clouds':
				return "ちぎれた雲<br>";
				break;
			case 'few clouds':
				return "少しの雲<br>";
				break;
			case 'light rain':
				return "小雨<br>";
				break;
			case 'light snow':
				return "小雪<br>";
				break;
			default:
				echo $description."<br>";
		}
}

function convert3($main){
	switch($main){
			case 'Clear':
				return "晴れ<img src=\"img/icon04.png\">";
				break;
			case 'Clouds':
				return "曇<img src=\"img/icon02.png\">";
				break;
			case 'Rain':
				return "雨<img src=\"img/icon03.png\">";
				break;
			case 'Snow':
				return "雪<img src=\"img/icon08.png\">";
				break;
			default:
				echo $main;
		}
}
?>

まーいいんじゃないでしょうか。

DBのテーブルとページ数が増えると、確かにフレームワークの方が楽に感じてきました。

全国の都市の緯度経度を設定する

基本的に国鉄の駅のlatitude, longitudeを設定していきます。
半分くらいしか行ったことなですね(照)

var markerData = [ 
  {
       name: '札幌',
       lat: 43.065381,
        lng: 141.334961,
        icon: icon[0]
 }, {
 	name: '釧路',
       lat: 42.990412,
        lng: 144.381878,
        icon: icon[1]
 }, {
 	name: '仙台',
       lat: 38.268215,
        lng: 140.869356,
        icon: icon[2]
 }, {
 	name: '新潟',
       lat: 37.916192,
        lng: 139.036413,
        icon: icon[3]
 }, {
 	name: '金沢',
       lat: 36.561325,
        lng: 136.656205,
        icon: icon[4]
 }, {
 	name: '東京',
       lat: 35.681167,
        lng: 139.767052,
        icon: icon[5]
 }, {
        name: '横浜',
     	lat: 35.469716,
        lng: 139.629184,
        icon: icon[6]
 }, {
 	name: '名古屋',
       lat: 35.181446,
        lng: 136.906398,
        icon: icon[7]
 }, {
 	name: '大阪',
       lat: 34.702485,
        lng: 135.495951,
        icon: icon[8]
 }, {
 	name: '広島',
       lat: 34.39779,
        lng: 132.47534,
        icon: icon[9]
 }, {
 	name: '高知',
       lat: 33.567697,
        lng: 133.546985,
        icon: icon[10]
 }, {
 	name: '福岡',
       lat: 33.590355,
        lng: 130.401716,
        icon: icon[11]
 }, {
 	name: '鹿児島',
       lat: 31.601456,
        lng: 130.563018,
        icon: icon[12]
 }, {
        name: '那覇',
     lat: 26.212312,
      lng: 127.679157,
      icon: icon[13]
 }
];

綺麗に反映されているようです。

Controllerで全国の都市のテーブルを引っ張る

NationwidesController.phpを作ります。

<?php

namespace App\Controller;

use Cake\ORM\TableRegistry;  

class NationwidesController extends AppController
{	
	public function initialize()
    {
         parent::initialize();
         $this->Sapporos = TableRegistry::get('sapporos');
         $this->Kushiros = TableRegistry::get('kushiros');
         $this->Sendais = TableRegistry::get('sendais');
         $this->Niigatas = TableRegistry::get('niigatas');
         $this->Kanazawas = TableRegistry::get('kanazawas');
         $this->Yokohamas = TableRegistry::get('Yokohamas');
         $this->Marunouchis = TableRegistry::get('Marunouchis');
         $this->Nagoyas = TableRegistry::get('nagoyas');
         $this->Osakas = TableRegistry::get('osakas');
         $this->Hiroshimas = TableRegistry::get('hiroshimas');
         $this->Kochis = TableRegistry::get('kochis');
         $this->Fukuokas = TableRegistry::get('fukuokas');
         $this->Kagoshimas = TableRegistry::get('kagoshimas');
         $this->Nahas = TableRegistry::get('nahas');
    }

	public function index()
	{
		$this->viewBuilder()->layout('my_layout');
		$now = date("Y-m-d H:i:s");
		$params = array(
		    'conditions' => array(
		        'forecast >' => $now, 
		    ),
		);
		$sapporos = $this->Sapporos->find('all', $params)->limit(8);
		$kushiros = $this->Kushiros->find('all', $params)->limit(8);
		$sendais = $this->Sendais->find('all', $params)->limit(8);
		$niigatas = $this->Niigatas->find('all', $params)->limit(8);
		$kanazawas = $this->Kanazawas->find('all', $params)->limit(8);
		$marunouchis = $this->Marunouchis->find('all', $params)->limit(8);
		$yokohamas = $this->Yokohamas->find('all', $params)->limit(8);
		$nagoyas = $this->Nagoyas->find('all', $params)->limit(8);
		$osakas = $this->Osakas->find('all', $params)->limit(8);
		$hiroshimas = $this->Hiroshimas->find('all', $params)->limit(8);
		$kochis = $this->Kochis->find('all', $params)->limit(8);
		$fukuokas = $this->Fukuokas->find('all', $params)->limit(8);
		$kagoshimas = $this->Kagoshimas->find('all', $params)->limit(8);
		$nahas = $this->Nahas->find('all', $params)->limit(8);
		$this->set(compact('sapporos'));
		$this->set(compact('kushiros'));
		$this->set(compact('sendais'));
		$this->set(compact('niigatas'));
		$this->set(compact('kanazawas'));	
		$this->set(compact('marunouchis'));
		$this->set(compact('yokohamas'));
		$this->set(compact('nagoyas'));
		$this->set(compact('osakas'));
		$this->set(compact('hiroshimas'));
		$this->set(compact('kochis'));
		$this->set(compact('fukuokas'));
		$this->set(compact('kagoshimas'));
		$this->set(compact('nahas'));
	}
}
?>

viewにちゃんとデータが入っているようです。
釧路-7.5°C って本当でしょうか? ん?

全国の都市のテーブルをつくる

city.list.jsonから、都市名を抽出します。
Sapporo-shi, Kushiro, Sendai-shi, Niigata-shi,Kanazawa-shi
Nagoya-shi, Osaka-shi, Hiroshima-shi, Kochi-shi, Fukuoka-shi
Kagoshima-shi, Naha-shi

それぞれ、cake用に、sをつけてテーブルをつくります。

create table weather.nahas(
	id int unsigned auto_increment primary key,
	forecast datetime,
	main varchar(255),
	description varchar(255),
	temp float,
	humidity int,
	cloud int,
	speed float,
	created datetime default null
);

sapporos, kushiros, sendais, niigatas, kanazawas, nagoyas,
osakas, hiroshimas, kochis, fukuokas, kagoshimas, nahas

kushiroは二つあるので注意が必要ですね。longitudeが144の方が北海度の釧路です。

{
    "id": 2129376,
    "name": "Kushiro",
    "country": "JP",
    "coord": {
      "lon": 144.374725,
      "lat": 42.974998
    }

{
    "id": 1858025,
    "name": "Kushiro",
    "country": "JP",
    "coord": {
      "lon": 135.416672,
      "lat": 34.799999
    }

テーブルが出来たので、データをDBに入れていきます。
まず、sapporoから。

問題なく入ってますね。

問題なさそうなので、一気に入れます。と思ったら、早速レコードに重複が出たのでやり直しのようです。(涙)
forecast datetime unique 修正にして、改めて一気にいれます。

<?php
require "sapporos.php";
require "kushiros.php";
require "sendais.php";
require "niigatas.php";
require "kanazawas.php";
require "marunouchis.php";
require "yokohamas.php";
require "nagoyas.php";
require "osakas.php";
require "hiroshimas.php";
require "kochis.php";
require "fukuokas.php";
require "kagoshimas.php";
require "nahas.php";
?>

面倒そうでしたが、やってみると案外サクサクいきましたね。