mirror of
https://github.com/PlaneQuery/OpenAirframes.git
synced 2026-07-25 11:10:49 +02:00
refactor: move FAA data conversion logic to a separate function
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
import zipfile
|
||||||
|
import pandas as pd
|
||||||
|
from faa_aircraft_registry import read
|
||||||
|
|
||||||
|
def convert_faa_master_txt_to_csv(zip_path: Path, csv_path: Path) -> None:
|
||||||
|
with zipfile.ZipFile(zip_path) as z:
|
||||||
|
registrations = read(z)
|
||||||
|
|
||||||
|
df = pd.DataFrame(registrations['master'].values())
|
||||||
|
col = "transponder_code_hex"
|
||||||
|
df = df[[col] + [c for c in df.columns if c != col]]
|
||||||
|
df = df.rename(columns={"transponder_code_hex": "icao"})
|
||||||
|
registrant = pd.json_normalize(df["registrant"]).add_prefix("registrant_")
|
||||||
|
df = df.drop(columns="registrant").join(registrant)
|
||||||
|
df = df.rename(columns={"aircraft_type": "aircraft_type_2"})
|
||||||
|
aircraft = pd.json_normalize(df["aircraft"]).add_prefix("aircraft_")
|
||||||
|
df = df.drop(columns="aircraft").join(aircraft)
|
||||||
|
df = df.rename(columns={"engine_type": "engine_type_2"})
|
||||||
|
engine = pd.json_normalize(df["engine"].where(df["engine"].notna(), {})).add_prefix("engine_")
|
||||||
|
df = df.drop(columns="engine").join(engine)
|
||||||
|
df = df.sort_values(by=["icao"])
|
||||||
|
df.to_csv(csv_path, index=False)
|
||||||
+2
-21
@@ -1,6 +1,3 @@
|
|||||||
from faa_aircraft_registry import read
|
|
||||||
import pandas as pd
|
|
||||||
import zipfile
|
|
||||||
import zipfile
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
@@ -28,21 +25,5 @@ with urlopen(req, timeout=120) as r:
|
|||||||
body = r.read()
|
body = r.read()
|
||||||
zip_path.write_bytes(body)
|
zip_path.write_bytes(body)
|
||||||
|
|
||||||
with zipfile.ZipFile(zip_path) as z:
|
from derive_from_faa_master_txt import convert_faa_master_txt_to_csv
|
||||||
registrations = read(z)
|
convert_faa_master_txt_to_csv(zip_path, csv_path)
|
||||||
|
|
||||||
df = pd.DataFrame(registrations['master'].values())
|
|
||||||
col = "transponder_code_hex"
|
|
||||||
df = df[[col] + [c for c in df.columns if c != col]]
|
|
||||||
df = df.rename(columns={"transponder_code_hex": "icao"})
|
|
||||||
registrant = pd.json_normalize(df["registrant"]).add_prefix("registrant_")
|
|
||||||
df = df.drop(columns="registrant").join(registrant)
|
|
||||||
df = df.rename(columns={"aircraft_type": "aircraft_type_2"})
|
|
||||||
aircraft = pd.json_normalize(df["aircraft"]).add_prefix("aircraft_")
|
|
||||||
df = df.drop(columns="aircraft").join(aircraft)
|
|
||||||
df = df.rename(columns={"engine_type": "engine_type_2"})
|
|
||||||
engine = pd.json_normalize(df["engine"].where(df["engine"].notna(), {})).add_prefix("engine_")
|
|
||||||
df = df.drop(columns="engine").join(engine)
|
|
||||||
df = df.sort_values(by=["icao"])
|
|
||||||
df.to_csv(csv_path, index=False)
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user