Skip to content

Instantly share code, notes, and snippets.

@BrianValente
Created October 20, 2019 12:58
Show Gist options
  • Save BrianValente/7b9d3681211566529bb4a9a500b999cf to your computer and use it in GitHub Desktop.
Save BrianValente/7b9d3681211566529bb4a9a500b999cf to your computer and use it in GitHub Desktop.
<?php
require __DIR__.'/vendor/autoload.php';
function dateToIso8601($date) {
return $date->format("Ymd") . "T" . $date->format("His") . "Z";
}
function getCurrentDateUtc() {
return new DateTime("now", new \DateTimeZone("UTC"));
}
function array_to_xml($student_info, &$xml_student_info, $root = true) {
foreach($student_info as $key => $value) {
if(is_array($value)) {
$key = is_numeric($key) ? ($root? "Satellite" : "Coordinate") : $key;
$subnode = $xml_student_info->addChild("$key");
array_to_xml($value, $subnode, false);
}
else {
$key = is_numeric($key) ? "item$key" : $key;
$xml_student_info->addChild("$key","$value");
}
}
}
$observatories = file_get_contents("https://sscweb.sci.gsfc.nasa.gov/WS/sscr/2/observatories");
$observatoriesObj = simplexml_load_string($observatories) or die("Error: Cannot create object");
$activeObservatories =
from($observatoriesObj)
//->where('$ob ==> new DateTime($ob["StartTime"]) < $dateMinusHour && new DateTime($ob["EndTime"]) > $datePlusHour')
->where(function($item) {
$obStartDate = DateTime::createFromFormat("Y-m-d\TH:i:s.u\Z", $item->StartTime);
$obEndDate = DateTime::createFromFormat("Y-m-d\TH:i:s.u\Z", $item->EndTime);
$datePlusHour = getCurrentDateUtc()->add(new DateInterval('PT1H'));
$dateMinusHour = getCurrentDateUtc()->sub(new DateInterval('PT1H'));
return $obStartDate < $dateMinusHour && $obEndDate > $datePlusHour;
})
->toList();
$activeObservatoriesChunks = array_chunk($activeObservatories, 40);
$satellites = array();
foreach ($activeObservatoriesChunks as $activeObservatoriesChunk) {
$idList =
from($activeObservatoriesChunk)
->select(function($ob) {
return $ob->Id;
})
->toString(",");
$datePlusHour = dateToIso8601(getCurrentDateUtc()->add(new DateInterval('PT1H')));
$dateMinusHour = dateToIso8601(getCurrentDateUtc()->sub(new DateInterval('PT1H')));
$endpoint = "https://sscweb.sci.gsfc.nasa.gov/WS/sscr/2/locations/$idList/$dateMinusHour,$datePlusHour/gse/";
$rawResponse = file_get_contents($endpoint);
$obj = simplexml_load_string($rawResponse) or die("Error: Cannot create object");
foreach ($obj->Result->Data as $satellite) {
$observatory =
from($activeObservatories)
->where(function($ob) use($satellite) {
// print_r($ob);
// die;
return strcmp($ob->Id, "$satellite->Id") == 0;
})
->firstOrDefault();
if (!isset($observatory->Id)) {
error_log("There was an error looking for the observatory ID $satellite->Id");
die;
}
$sat = [
'Id' => (string) $observatory->Id,
'Name' => (string) $observatory->Name,
'ResourceId' => (string) $observatory->ResourceId,
'Coordinates' => array(),
];
$coordsCount = count($satellite->Time);
for ($i = 0; $i < $coordsCount; $i++) {
$sat["Coordinates"][] = [
"Longitude" => (float)$satellite->Coordinates->Longitude[$i],
"Latitude" => (float)$satellite->Coordinates->Latitude[$i],
"Time" => (string)$satellite->Time[$i],
"LocalTime" => (string)$satellite->Coordinates->LocalTime[$i]
];
}
$satellites[] = $sat;
}
}
$xml = new SimpleXMLElement('<Satellites/>');
array_to_xml($satellites, $xml);
$xmlDocument = new DOMDocument('1.0');
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->formatOutput = true;
$xmlDocument->loadXML($xml->asXML());
file_put_contents("satellites.xml", $xmlDocument->saveXML());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment