first commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Import satellite standard magnitudes from the qs.mag file (Mike McCants).
|
||||
*
|
||||
* Source: https://www.mmccants.org/programs/sgp4/tle_satinfo.txt
|
||||
* Mirror: https://raw.githubusercontent.com/barolfe/satellite-tracker/master/qs.mag
|
||||
*
|
||||
* Format (fixed-width):
|
||||
* 0-4 NORAD catalog number
|
||||
* 6 Flag: 'd'=decayed, 't'=tumbling, ' '=normal
|
||||
* 8-14 International designation
|
||||
* 15-28 Satellite name
|
||||
* ~29+ Standard magnitude (at 1000 km range, 50% illumination phase)
|
||||
* followed by sizes and RCS
|
||||
*
|
||||
* Usage:
|
||||
* php import_qsmag.php [path-to-qs.mag]
|
||||
* php import_qsmag.php (downloads from mirror)
|
||||
*/
|
||||
|
||||
$config = require __DIR__ . '/../../config/database.php';
|
||||
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;dbname=%s;charset=%s',
|
||||
$config['host'],
|
||||
$config['dbname'],
|
||||
$config['charset'] ?? 'utf8mb4'
|
||||
);
|
||||
|
||||
$pdo = new PDO($dsn, $config['user'], $config['pass'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
|
||||
// Quelldatei bestimmen
|
||||
$sourceFile = $argv[1] ?? null;
|
||||
$tempFile = null;
|
||||
|
||||
if ($sourceFile !== null) {
|
||||
if (!file_exists($sourceFile)) {
|
||||
exit("Datei nicht gefunden: {$sourceFile}\n");
|
||||
}
|
||||
$content = file_get_contents($sourceFile);
|
||||
} else {
|
||||
echo "Lade qs.mag von Mirror...\n";
|
||||
$url = 'https://raw.githubusercontent.com/barolfe/satellite-tracker/master/qs.mag';
|
||||
$content = @file_get_contents($url);
|
||||
if ($content === false) {
|
||||
exit("Download fehlgeschlagen. Bitte qs.mag manuell herunterladen und als Argument uebergeben.\n");
|
||||
}
|
||||
echo "Download OK (" . strlen($content) . " Bytes).\n";
|
||||
}
|
||||
|
||||
$lines = explode("\n", str_replace("\r\n", "\n", $content));
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE `sat_satellites`
|
||||
SET `std_mag` = :std_mag
|
||||
WHERE `norad_cat_id` = :norad
|
||||
");
|
||||
|
||||
$updated = 0;
|
||||
$skipped = 0;
|
||||
$noMag = 0;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// Mindestlaenge und Header-Zeile ueberspringen
|
||||
if (strlen($line) < 20) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$norad = (int)substr($line, 0, 5);
|
||||
if ($norad === 0) {
|
||||
continue; // Header oder Leerzeile
|
||||
}
|
||||
|
||||
// Magnitude steht nach dem Namensfeld (ab ~Position 25),
|
||||
// als erster Float-Wert (kann negativ sein, z.B. -1.5 fuer ISS)
|
||||
if (!preg_match('/^.{25,}\s+([-\d]+\.?\d*)\s/', $line, $m)) {
|
||||
$noMag++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$stdMag = (float)$m[1];
|
||||
|
||||
$stmt->execute([':std_mag' => $stdMag, ':norad' => $norad]);
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$updated++;
|
||||
} else {
|
||||
$skipped++; // Satellit nicht in DB
|
||||
}
|
||||
}
|
||||
|
||||
echo "Fertig.\n";
|
||||
echo " Aktualisiert: {$updated}\n";
|
||||
echo " Nicht in DB: {$skipped}\n";
|
||||
echo " Ohne Mag: {$noMag}\n";
|
||||
Reference in New Issue
Block a user