|
| 1 | +import base64 |
1 | 2 | import re |
| 3 | +import tempfile |
2 | 4 | import warnings |
3 | 5 | import xml.etree.ElementTree as ET |
4 | 6 | from abc import ABC, abstractmethod |
5 | 7 | from functools import cached_property |
6 | 8 | from os import path |
7 | 9 |
|
8 | 10 | import numpy as np |
| 11 | +import requests |
9 | 12 |
|
10 | 13 | from ..mathutils.function import Function, funcify_method |
11 | 14 | from ..plots.motor_plots import _MotorPlots |
@@ -1914,6 +1917,62 @@ def load_from_rse_file( |
1914 | 1917 | coordinate_system_orientation=coordinate_system_orientation, |
1915 | 1918 | ) |
1916 | 1919 |
|
| 1920 | + @staticmethod |
| 1921 | + def load_from_thrustcurve_api(name: str, **kwargs): |
| 1922 | + """ |
| 1923 | + Creates a Motor instance by downloading a .eng file from the ThrustCurve API |
| 1924 | + based on the given motor name. |
| 1925 | +
|
| 1926 | + Parameters |
| 1927 | + ---------- |
| 1928 | + name : str |
| 1929 | + The motor name according to the API (e.g., "Cesaroni_M1670"). |
| 1930 | + **kwargs : |
| 1931 | + Additional arguments passed to the Motor constructor, such as dry_mass, nozzle_radius, etc. |
| 1932 | +
|
| 1933 | + Returns |
| 1934 | + ------- |
| 1935 | + instance : cls |
| 1936 | + A new Motor instance initialized using the downloaded .eng file. |
| 1937 | + """ |
| 1938 | + |
| 1939 | + base_url = "https://www.thrustcurve.org/api/v1" |
| 1940 | + |
| 1941 | + # Step 1. Search motor |
| 1942 | + response = requests.get(f"{base_url}/search.json", params={"commonName": name}) |
| 1943 | + response.raise_for_status() |
| 1944 | + data = response.json() |
| 1945 | + |
| 1946 | + if not data.get("results"): |
| 1947 | + print("No motor found.") |
| 1948 | + return None |
| 1949 | + |
| 1950 | + motor = data["results"][0] |
| 1951 | + motor_id = motor["motorId"] |
| 1952 | + designation = motor["designation"].replace("/", "-") |
| 1953 | + print(f"Motor found: {designation} ({motor['manufacturer']})") |
| 1954 | + |
| 1955 | + # Step 2. Download the .eng file |
| 1956 | + dl_response = requests.get( |
| 1957 | + f"{base_url}/download.json", |
| 1958 | + params={"motorIds": motor_id, "format": "RASP", "data": "file"}, |
| 1959 | + ) |
| 1960 | + dl_response.raise_for_status() |
| 1961 | + data = dl_response.json() |
| 1962 | + |
| 1963 | + data_base64 = data["results"][0]["data"] |
| 1964 | + data_bytes = base64.b64decode(data_base64) |
| 1965 | + |
| 1966 | + # Step 3. Create the motor from the .eng file |
| 1967 | + |
| 1968 | + with tempfile.NamedTemporaryFile(suffix=".eng", delete=True) as tmp_file: |
| 1969 | + tmp_file.write(data_bytes) |
| 1970 | + tmp_file.flush() |
| 1971 | + |
| 1972 | + motor = GenericMotor.load_from_eng_file(tmp_file.name, **kwargs) |
| 1973 | + |
| 1974 | + return motor |
| 1975 | + |
1917 | 1976 | def all_info(self): |
1918 | 1977 | """Prints out all data and graphs available about the Motor.""" |
1919 | 1978 | # Print motor details |
|
0 commit comments