Skip to content

Commit

Permalink
Added by country
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecardleitao committed Mar 4, 2024
1 parent 4b76979 commit 59431d6
Showing 1 changed file with 99 additions and 7 deletions.
106 changes: 99 additions & 7 deletions analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ def set_size(width: float, fraction=1):
FIG_SIZE = set_size(600)


STATUS_PATH = "https://private-jets.fra1.digitaloceanspaces.com/leg/v1/status.json"
LEGS_PATH = "https://private-jets.fra1.digitaloceanspaces.com/leg/v1/all.csv"
VERSION = "v1"
STATUS_PATH = f"https://private-jets.fra1.digitaloceanspaces.com/leg/{VERSION}/status.json"
LEGS_PATH = f"https://private-jets.fra1.digitaloceanspaces.com/leg/{VERSION}/all.csv"
AIRCRAFT_PATH = "https://private-jets.fra1.digitaloceanspaces.com/private_jets/all.csv"
duckdb.sql(
f"""
CREATE TEMP TABLE "legs" AS (
SELECT
*
FROM read_csv_auto('{LEGS_PATH}', header = true)
WHERE date_part('year', "start") = 2023
FROM
read_csv_auto('{LEGS_PATH}', header = true)
WHERE
date_part('year', "start") = 2023
)
"""
)
Expand Down Expand Up @@ -90,6 +93,93 @@ def aircraft():
fig.savefig("results/aircraft.png", dpi=300)


def aircraft_by_country():
x = duckdb.sql(
f"""
SELECT
country,COUNT(*) AS aircraft
FROM read_csv_auto('{AIRCRAFT_PATH}', header = true)
GROUP BY country
ORDER BY aircraft DESC
"""
).fetchall()

t = list(range(len(x)))
number = [x[1] for x in x]
percentage = [x[1] / sum(number) * 100 for x in x]

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=FIG_SIZE, sharex=True)

#fig.supylabel("Number of aircraft")
ax2.set_xlabel("Country (rank by number)")
ax1.grid(linestyle="dotted")
ax2.grid(linestyle="dotted")

ax1.annotate(x[0][0], xy=(t[0] + 1, number[0]))
for i in range(1, 5):
ax2.annotate(x[i][0], xy=(t[i] + 1, number[i]))
ax1.plot(t, number, "o")
ax2.plot(t, number, "o")
# ax1.tick_params(axis="y", labelcolor=color)
ax1.set_ylim(0.6 * sum(number), 1.0 * sum(number))
ax2.set_ylim(0, 0.04 * sum(number))

# hide the spines between ax and ax2
ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)
ax1.xaxis.tick_top()
ax1.tick_params(labeltop=False) # don't put tick labels at the top
ax2.xaxis.tick_bottom()

d = 0.5 # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(
marker=[(-1, -d), (1, d)],
markersize=12,
linestyle="none",
color="k",
mec="k",
mew=1,
clip_on=False,
)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)

ax3 = ax1.twinx()

fig.text(
0.03,
0.5,
"Number of aircraft",
ha="center",
va="center",
rotation="vertical",
)
fig.text(
0.97,
0.5,
"Market share (%)",
ha="center",
va="center",
rotation="vertical",
)

ax3.plot(t, percentage, "o")
ax3.set_ylim(60, 100)

ax4 = ax2.twinx()

ax4.plot(t, percentage, "o")
ax4.set_ylim(0.0, 4)

ax4.set_zorder(1) # default zorder is 0 for ax1 and ax2
ax4.patch.set_visible(False) # prevents ax1 from hiding ax2

fig.subplots_adjust(hspace=0.05)
fig.savefig("results/country.png", dpi=300, bbox_inches="tight")

return


def ranked_legs():
x = duckdb.sql(
f"""
Expand Down Expand Up @@ -251,6 +341,8 @@ def per_month():

fig.tight_layout()
fig.savefig("results/timeseries_emissions.png", dpi=300)
print(f"{sum(emissions)} kt of CO2e")
print(f"{sum(emissions) / 3.0 / 1.68} Mt of CO2")


def hours_per_model():
Expand Down Expand Up @@ -300,6 +392,7 @@ def hours_per_model():

def base_analysis():
aircraft()
aircraft_by_country()
ranked_legs()
ranked_hours()
histogram_hours()
Expand All @@ -312,12 +405,11 @@ def base_analysis():
def distribution():
x = duckdb.sql(
f"""
INSTALL spatial;
LOAD spatial;
SELECT
epoch("end" - "start") / 60 / 60 AS "flying_time_k_hours"
, distance
FROM "legs"
USING SAMPLE 10%
"""
).fetchall()

Expand All @@ -333,5 +425,5 @@ def distribution():
plt.savefig("results/dist.png", dpi=300)


# distribution()
distribution()
base_analysis()

0 comments on commit 59431d6

Please sign in to comment.