Files
radarsargasses/backend/src/Command/IngestSentinelCommand.php
2026-04-14 16:26:20 -04:00

95 lines
3.3 KiB
PHP

<?php
namespace App\Command;
use App\Service\Ingestion\IngestionService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:ingest-sentinel',
description: 'Ingestion des données Sentinel-2 pour la détection de sargasses',
)]
class IngestSentinelCommand extends Command
{
// Zone d'intérêt : archipel guadeloupéen uniquement.
// Bbox : couvre Les Saintes, La Désirade, Grande-Terre, Basse-Terre,
// Marie-Galante + ~350 km d'océan à l'est pour détecter les sargasses en approche.
private const ZONES = [
'guadeloupe' => [-62.50, 15.70, -58.50, 17.00],
];
public function __construct(private IngestionService $ingestionService)
{
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('date', 'd', InputOption::VALUE_OPTIONAL,
'Date à traiter (YYYY-MM-DD). Défaut : hier.', null)
->addOption('zone', 'z', InputOption::VALUE_OPTIONAL,
'Zone spécifique. Défaut : toutes les zones.', null)
->addOption('source', 's', InputOption::VALUE_OPTIONAL,
'Source satellite : Sentinel-2, Sentinel-3, ou auto (S2 avec fallback S3)', 'auto');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$dateStr = $input->getOption('date');
$date = $dateStr
? new \DateTimeImmutable($dateStr)
: new \DateTimeImmutable('yesterday');
$source = $input->getOption('source');
$zones = self::ZONES;
if ($zoneName = $input->getOption('zone')) {
if (!isset(self::ZONES[$zoneName])) {
$io->error(sprintf(
"Zone inconnue : \"%s\". Zones disponibles : %s",
$zoneName,
implode(', ', array_keys(self::ZONES))
));
return Command::FAILURE;
}
$zones = [$zoneName => self::ZONES[$zoneName]];
}
$io->title(sprintf('Ingestion %s — %s', $source, $date->format('Y-m-d')));
$errors = 0;
foreach ($zones as $name => $bbox) {
$io->section("Zone : {$name}");
try {
if ($source === 'auto') {
$this->ingestionService->ingestWithFallback($bbox, $date);
} else {
$collection = match (strtolower($source)) {
'sentinel-3' => 'sentinel-3-olci',
default => 'sentinel-2-l2a',
};
$this->ingestionService->ingest($bbox, $date, $source, $collection);
}
$io->success("OK — {$name}");
} catch (\Throwable $e) {
$io->error("ÉCHEC — {$name} : " . $e->getMessage());
$errors++;
}
}
if ($errors > 0) {
$io->warning("{$errors} zone(s) en erreur.");
}
return $errors === 0 ? Command::SUCCESS : Command::FAILURE;
}
}