Skip to content

Commit ff47fd1

Browse files
committed
Improves orbital elements import performance
Streams large remote element files and performs batched (500-row) inserts to reduce memory usage and speed up imports. Adds basic download error handling and preserves table truncation to ensure clean, consistent imports.
1 parent 0f212cf commit ff47fd1

2 files changed

Lines changed: 66 additions & 42 deletions

File tree

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to `laravel-astronomy-library` will be documented in this file.
44

5+
## Version 6.7.7
6+
7+
Added:
8+
9+
- Artisan command `astronomy:updateOrbitalElements` (`src/deepskylog/AstronomyLibrary/Commands/UpdateOrbitalElements.php`): downloads JPL small-body element files (`ELEMENTS.COMET` and `ELEMENTS.NUMBR`), parses comet and asteroid orbital elements and imports them into the `CometsOrbitalElements` and `AsteroidsOrbitalElements` models. Performs epoch conversion (adds 2400000.5), truncates target tables before import, and uses batched inserts (500 rows) for performance. Includes basic error handling for download failures.
10+
11+
512
## Version 6.7.6
613

714
Changed:

src/deepskylog/AstronomyLibrary/Commands/UpdateOrbitalElements.php

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,20 @@ public function __construct()
3737
*/
3838
public function handle()
3939
{
40-
// Download new orbital elements for comets from the JPL website
41-
$contents = file_get_contents(
42-
'https://ssd.jpl.nasa.gov/dat/ELEMENTS.COMET'
43-
);
40+
// Download new orbital elements for comets from the JPL website (stream line by line)
41+
$handle = fopen('https://ssd.jpl.nasa.gov/dat/ELEMENTS.COMET', 'r');
42+
if ($handle === false) {
43+
$this->error('Failed to download comet orbital elements.');
44+
return 1;
45+
}
4446

4547
// Remove the old entries
4648
CometsOrbitalElements::truncate();
4749

4850
$cnt = 0;
49-
// Loop over the orbital elements line by line
50-
foreach (preg_split("/((\r?\n)|(\r\n?))/", $contents) as $line) {
51+
$batch = [];
52+
while (($line = fgets($handle)) !== false) {
53+
$line = rtrim($line, "\r\n");
5154
if ($cnt > 1) {
5255
// The first 43 characters are the name
5356
$name = trim(substr($line, 0, 43));
@@ -69,35 +72,44 @@ public function handle()
6972
$ref = trim(substr($line, 120));
7073

7174
if ($name != '') {
72-
CometsOrbitalElements::create(
73-
[
74-
'name' => $name,
75-
'epoch' => $epoch,
76-
'q' => $q,
77-
'e' => $e,
78-
'i' => $i,
79-
'w' => $w,
80-
'node' => $node,
81-
'Tp' => $Tp,
82-
'ref' => $ref,
83-
]
84-
);
75+
$batch[] = [
76+
'name' => $name,
77+
'epoch' => $epoch,
78+
'q' => $q,
79+
'e' => $e,
80+
'i' => $i,
81+
'w' => $w,
82+
'node' => $node,
83+
'Tp' => $Tp,
84+
'ref' => $ref,
85+
];
86+
if (count($batch) >= 500) {
87+
CometsOrbitalElements::insert($batch);
88+
$batch = [];
89+
}
8590
}
8691
}
8792
$cnt++;
8893
}
94+
fclose($handle);
95+
if (!empty($batch)) {
96+
CometsOrbitalElements::insert($batch);
97+
}
8998

90-
// Download new orbital elements for asteroids from the JPL website
91-
$contents = file_get_contents(
92-
'https://ssd.jpl.nasa.gov/dat/ELEMENTS.NUMBR'
93-
);
99+
// Download new orbital elements for asteroids from the JPL website (stream line by line)
100+
$handle = fopen('https://ssd.jpl.nasa.gov/dat/ELEMENTS.NUMBR', 'r');
101+
if ($handle === false) {
102+
$this->error('Failed to download asteroid orbital elements.');
103+
return 1;
104+
}
94105

95106
// Remove the old entries
96107
AsteroidsOrbitalElements::truncate();
97108

98109
$cnt = 0;
99-
// Loop over the orbital elements line by line
100-
foreach (preg_split("/((\r?\n)|(\r\n?))/", $contents) as $line) {
110+
$batch = [];
111+
while (($line = fgets($handle)) !== false) {
112+
$line = rtrim($line, "\r\n");
101113
if ($cnt > 1) {
102114
// Character 0 - 6 is the number
103115
$number = intval(substr($line, 0, 6));
@@ -125,26 +137,31 @@ public function handle()
125137
$ref = trim(substr($line, 107));
126138

127139
if ($name != '') {
128-
AsteroidsOrbitalElements::create(
129-
[
130-
'number' => $number,
131-
'name' => $name,
132-
'epoch' => $epoch,
133-
'a' => $a,
134-
'e' => $e,
135-
'i' => $i,
136-
'w' => $w,
137-
'node' => $node,
138-
'M' => $M,
139-
'H' => $H,
140-
'G' => $G,
141-
'Tp' => $Tp,
142-
'ref' => $ref,
143-
]
144-
);
140+
$batch[] = [
141+
'number' => $number,
142+
'name' => $name,
143+
'epoch' => $epoch,
144+
'a' => $a,
145+
'e' => $e,
146+
'i' => $i,
147+
'w' => $w,
148+
'node' => $node,
149+
'M' => $M,
150+
'H' => $H,
151+
'G' => $G,
152+
'ref' => $ref,
153+
];
154+
if (count($batch) >= 500) {
155+
AsteroidsOrbitalElements::insert($batch);
156+
$batch = [];
157+
}
145158
}
146159
}
147160
$cnt++;
148161
}
162+
fclose($handle);
163+
if (!empty($batch)) {
164+
AsteroidsOrbitalElements::insert($batch);
165+
}
149166
}
150167
}

0 commit comments

Comments
 (0)