Skip to content

Commit 73b3dff

Browse files
authored
Merge pull request #2 from mudhoney/feature/faster-multibatch
Feature/faster multibatch
2 parents ed43d50 + 1f687a6 commit 73b3dff

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

app/hgs2hpc.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import astropy.units as u
44
from sunpy.coordinates import frames, transform_with_sun_center
55
from sunpy.physics.differential_rotation import solar_rotate_coordinate
6+
from typing import List, Dict
67

78
from frames import get_helioviewer_frame, get_earth_frame
89

@@ -26,6 +27,7 @@ def hgs2hpc(lat: float, lon: float, coord_time: Time, target: Time) -> SkyCoord:
2627
Desired observation time
2728
"""
2829
hv_frame = get_helioviewer_frame(target)
30+
2931
with transform_with_sun_center():
3032
coord = SkyCoord(
3133
lon * u.deg,
@@ -37,3 +39,43 @@ def hgs2hpc(lat: float, lon: float, coord_time: Time, target: Time) -> SkyCoord:
3739
hpc = coord.transform_to(earth_frame)
3840
# Then apply the rotation as seen from Helioviewer
3941
return solar_rotate_coordinate(hpc, hv_frame.observer)
42+
43+
44+
def hgs2hpc_batch(coordinates: List[Dict], target: Time) -> List[Dict]:
45+
"""
46+
Batch process multiple HGS to HPC coordinate transformations
47+
48+
Parameters
49+
----------
50+
coordinates : List[Dict]
51+
List of coordinate dictionaries with keys: lat, lon, coord_time
52+
target : Time
53+
Target observation time (same for all coordinates)
54+
55+
Returns
56+
-------
57+
List[Dict]
58+
List of results with keys: x, y
59+
"""
60+
if not coordinates:
61+
return []
62+
63+
hv_frame = get_helioviewer_frame(target)
64+
65+
with transform_with_sun_center():
66+
lats = [c["lat"] for c in coordinates]
67+
lons = [c["lon"] for c in coordinates]
68+
coord_time = [c["coord_time"] for c in coordinates]
69+
coord = SkyCoord(
70+
lons,
71+
lats,
72+
unit="deg,deg",
73+
frame=frames.HeliographicStonyhurst,
74+
obstime=coord_time,
75+
)
76+
# First convert to an hpc coordinate
77+
earth_frame = get_earth_frame(coord_time)
78+
hpc = coord.transform_to(earth_frame)
79+
# Then apply the rotation as seen from Helioviewer
80+
result = solar_rotate_coordinate(hpc, hv_frame.observer)
81+
return [{"x": c.Tx.value.item(), "y": c.Ty.value.item()} for c in result]

app/main.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi.middleware.cors import CORSMiddleware
55
from pydantic import Field
66

7-
from hgs2hpc import hgs2hpc
7+
from hgs2hpc import hgs2hpc, hgs2hpc_batch
88
from normalizer import normalize_hpc, gse_frame, jsonify_skycoord
99
from ephemeris import get_position
1010
from validation import AstropyTime, HvBaseModel
@@ -64,12 +64,15 @@ class Hgs2HpcBatchInput(HvBaseModel):
6464
)
6565
def _hgs2hpc_post(params: Hgs2HpcBatchInput):
6666
"Convert a latitude/longitude coordinate to the equivalent helioprojective coordinate at the given target time"
67-
coords = map(
68-
lambda c: hgs2hpc(c.lat, c.lon, c.coord_time, params.target), params.coordinates
69-
)
70-
return {
71-
"coordinates": [{"x": coord.Tx.value, "y": coord.Ty.value} for coord in coords]
72-
}
67+
# Prepare coordinates for batch processing
68+
coords_input = [
69+
{"lat": c.lat, "lon": c.lon, "coord_time": c.coord_time}
70+
for c in params.coordinates
71+
]
72+
73+
results = hgs2hpc_batch(coords_input, params.target)
74+
75+
return {"coordinates": results}
7376

7477

7578
class NormalizeHpcQueryParameters(HvBaseModel):

0 commit comments

Comments
 (0)