forked from contrailcirrus/pycontrails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults_malaga.py
235 lines (183 loc) · 9.13 KB
/
results_malaga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import os
import glob
def plot_flight_data(flight_dirs, output_dirs, engine_models):
# Storage for min/max values across flights
lat_min, lat_max = float('inf'), float('-inf')
lon_min, lon_max = float('inf'), float('-inf')
rf_lw_min, rf_lw_max = float('inf'), float('-inf')
rf_sw_min, rf_sw_max = float('inf'), float('-inf')
ef_min, ef_max = float('inf'), float('-inf')
# Storage for data to plot later
flight_data = []
# First pass: Load data and determine global min/max values
for flight_dir, engine_model in zip(flight_dirs, engine_models):
parquet_path = glob.glob(os.path.join(flight_dir, f'co_cont_{engine_model}_0_0.parquet'))[0]
csv_path = glob.glob(os.path.join(flight_dir, f'{engine_model}_SAF_0_A20N_full_WAR_0_climate.csv'))[0]
cocip_df = pd.read_parquet(parquet_path)
fcocip_df = pd.read_csv(csv_path)
# Latitude and longitude from both fcocip_df and cocip_df
lat_min = min(lat_min, cocip_df['latitude'].min(), fcocip_df['latitude'].min())
lat_max = max(lat_max, cocip_df['latitude'].max(), fcocip_df['latitude'].max())
lon_min = min(lon_min, cocip_df['longitude'].min(), fcocip_df['longitude'].min())
lon_max = max(lon_max, cocip_df['longitude'].max(), fcocip_df['longitude'].max())
rf_lw_min = min(rf_lw_min, cocip_df['rf_lw'].min())
rf_lw_max = max(rf_lw_max, cocip_df['rf_lw'].max())
rf_sw_min = min(rf_sw_min, cocip_df['rf_sw'].min())
rf_sw_max = max(rf_sw_max, cocip_df['rf_sw'].max())
ef_min = min(ef_min, cocip_df['ef'].min())
ef_max = max(ef_max, cocip_df['ef'].max())
flight_data.append((fcocip_df, cocip_df))
# Add buffer of 0.5 degrees to lat/lon limits
lat_min -= 0.5
lat_max += 0.5
lon_min -= 0.5
lon_max += 0.5
print(f"Buffered Latitude range: {lat_min} to {lat_max}")
print(f"Buffered Longitude range: {lon_min} to {lon_max}")
print(f"Global RF_LW range: {rf_lw_min} to {rf_lw_max}")
print(f"Global RF_SW range: {rf_sw_min} to {rf_sw_max}")
print(f"Global EF range: {ef_min} to {ef_max}")
# Second pass: Plotting with consistent limits
for (fcocip_df, cocip_df), output_dir, engine_model in zip(flight_data, output_dirs, engine_models):
os.makedirs(output_dir, exist_ok=True)
## Long Wave RF Plot
plt.figure()
ax1 = plt.axes()
ax1.plot(fcocip_df['longitude'], fcocip_df['latitude'], color='k', label='Flight path')
sc1 = ax1.scatter(
cocip_df['longitude'],
cocip_df['latitude'],
c=cocip_df['rf_lw'],
cmap='Reds',
vmin=rf_lw_min,
vmax=rf_lw_max,
label='Contrail LW RF (W/m2)'
)
ax1.set_xlim(lon_min, lon_max)
ax1.set_ylim(lat_min, lat_max)
ax1.legend()
plt.title("Long Wave Radiative Forcing of Contrail")
plt.colorbar(sc1, ax=ax1, label='rf_lw')
plt.savefig(os.path.join(output_dir, f'{engine_model}_SAF_0_cocip_lw_rf.png'))
plt.close()
## Short Wave RF Plot
plt.figure()
ax2 = plt.axes()
ax2.plot(fcocip_df['longitude'], fcocip_df['latitude'], color='k', label='Flight path')
sc2 = ax2.scatter(
cocip_df['longitude'],
cocip_df['latitude'],
c=cocip_df['rf_sw'],
cmap='Blues_r',
vmin=rf_sw_min,
vmax=rf_sw_max,
label='Contrail SW RF (W/m2)'
)
ax2.set_xlim(lon_min, lon_max)
ax2.set_ylim(lat_min, lat_max)
ax2.legend()
plt.title("Short Wave Radiative Forcing of Contrail")
plt.colorbar(sc2, ax=ax2, label='rf_sw')
plt.savefig(os.path.join(output_dir, f'{engine_model}_SAF_0_cocip_sw_rf.png'))
plt.close()
## Energy Forcing Evolution Plot
plt.figure()
ax3 = plt.axes()
ax3.plot(fcocip_df['longitude'], fcocip_df['latitude'], color='k', label='Flight path')
max_abs = max(abs(ef_min), abs(ef_max))
norm = mcolors.TwoSlopeNorm(vmin=-max_abs, vcenter=0.0, vmax=max_abs)
sc3 = ax3.scatter(
cocip_df['longitude'],
cocip_df['latitude'],
c=cocip_df['ef'],
cmap='coolwarm',
norm=norm,
alpha=0.8,
label="Contrail EF (J)"
)
ax3.set_xlim(lon_min, lon_max)
ax3.set_ylim(lat_min, lat_max)
ax3.legend()
plt.title("Contrail Energy Forcing Evolution")
cbar = plt.colorbar(sc3, ax=ax3, label='ef')
cbar.formatter.set_powerlimits((0, 0))
plt.savefig(os.path.join(output_dir, f'{engine_model}_SAF_0_cocip_ef_evolution.png'))
plt.close()
print("Plots saved in the corresponding directories.")
# Specify the directories containing the parquet and CSV files for the two flights you want to compare
prediction = 'mees'
weather_model = 'era5model'
engine_model_1 = 'GTF'
prediction_2 = 'mees'
weather_model_2 = 'era5'
engine_model_2 = 'GTF'
flight1_dir = f"main_results_figures/results/malaga/malaga/climate/{prediction}/{weather_model}"
flight2_dir = f"main_results_figures/results/malaga/malaga/climate/{prediction_2}/{weather_model_2}"
output1_dir = f"main_results_figures/figures/malaga/malaga/climate/{prediction}/{weather_model}/cocip/{engine_model_1}/era5/"
output2_dir = f"main_results_figures/figures/malaga/malaga/climate/{prediction_2}/{weather_model_2}/cocip/{engine_model_2}/era5"
plot_flight_data([flight1_dir, flight2_dir], [output1_dir, output2_dir], [engine_model_1, engine_model_2])
csv_path_1 = glob.glob(os.path.join(flight1_dir, f'{engine_model_1}_SAF_0_A20N_full_WAR_0_climate.csv'))[0]
df_1 = pd.read_csv(csv_path_1)
csv_path_2 = glob.glob(os.path.join(flight2_dir, f'{engine_model_2}_SAF_0_A20N_full_WAR_0_climate.csv'))[0]
df_2 = pd.read_csv(csv_path_2)
# Compute total sums for each variable in both datasets
total_fuel_flow_1 = df_1['fuel_flow'].sum() # Per engine
total_fuel_flow_gsp_2 = df_2['fuel_flow'].sum()
total_ei_nox_1 = df_1['ei_nox'].sum()
total_ei_nox_gsp_2 = df_2['ei_nox'].sum()
total_ei_nvpm_1 = df_1['nvpm_ei_n'].sum()
total_ei_nvpm_gsp_2 = df_2['nvpm_ei_n'].sum()
total_nox_1 = (df_1['ei_nox'] * df_1['fuel_flow'] ).sum()
total_nox_gsp_2 = (df_2['ei_nox'] * df_2['fuel_flow']).sum()
total_nvpm_1 = (df_1['nvpm_ei_n'] * (df_1['fuel_flow'] )).sum()
total_nvpm_gsp_2 = (df_2['nvpm_ei_n'] * df_2['fuel_flow']).sum()
# Compute percentage differences
percentage_differences = {
'Fuel Flow': ((total_fuel_flow_gsp_2 - total_fuel_flow_1) / total_fuel_flow_1) * 100,
'EI NOx': ((total_ei_nox_gsp_2 - total_ei_nox_1) / total_ei_nox_1) * 100,
'NOx': ((total_nox_gsp_2 - total_nox_1) / total_nox_1) * 100,
'EI nvPM': ((total_ei_nvpm_gsp_2 - total_ei_nvpm_1) / total_ei_nvpm_1) * 100,
'nvPM': ((total_nvpm_gsp_2 - total_nvpm_1) / total_nvpm_1) * 100
}
# Create a bar plot
plt.figure(figsize=(8, 6))
plt.bar(percentage_differences.keys(), percentage_differences.values())
plt.xlabel("Metric")
plt.ylabel("Percentage Difference (%)")
plt.title("Emissions - Relative Difference Compared To Baseline")
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.savefig(f'results_report/climate_sensitivity_chapter/{prediction}_{prediction_2}_{weather_model}_{weather_model_2}_emissions.png', format='png')
plt.show()
total_co2_impact_1 = df_1['accf_sac_co2_impact'].sum()
total_co2_impact_gsp_2 = df_2['accf_sac_co2_impact'].sum()
total_nox_impact_1 = df_1['accf_sac_nox_impact'].sum()
total_nox_impact_gsp_2 = df_2['accf_sac_nox_impact'].sum()
print(total_nox_impact_1)
print(total_nox_impact_gsp_2)
total_cocip_atr20_impact_1 = df_1['cocip_atr20'].sum()
total_cocip_atr20_impact_gsp_2 = df_2['cocip_atr20'].sum()
print(total_cocip_atr20_impact_1)
print(total_cocip_atr20_impact_gsp_2)
total_non_co2_impact_1 = df_1['accf_sac_nox_impact'].sum()+df_1['cocip_atr20'].sum()
total_non_co2_impact_gsp_2 = df_2['accf_sac_nox_impact'].sum()+df_2['cocip_atr20'].sum()
total_impact_1 =df_1['accf_sac_nox_impact'].sum()+df_1['cocip_atr20'].sum()+df_1['accf_sac_co2_impact'].sum()
total_impact_gsp_2 =df_2['accf_sac_nox_impact'].sum()+df_2['cocip_atr20'].sum()+df_2['accf_sac_co2_impact'].sum()
impact_labels = ['CO2', 'NOx', 'Contrails', 'Non-CO2', 'Total Climate Impact']
percentage_climate_differences = [
((total_co2_impact_gsp_2 - total_co2_impact_1) / total_co2_impact_1) * 100,
((total_nox_impact_gsp_2 - total_nox_impact_1) / total_nox_impact_1) * 100,
((total_cocip_atr20_impact_gsp_2 - total_cocip_atr20_impact_1) / total_cocip_atr20_impact_1) * 100,
((total_non_co2_impact_gsp_2 - total_non_co2_impact_1) / total_non_co2_impact_1) * 100,
((total_impact_gsp_2 - total_impact_1) / total_impact_1) * 100
]
plt.figure(figsize=(8, 6))
plt.bar(impact_labels, percentage_climate_differences)
plt.xlabel("Metric")
plt.ylabel("Percentage Difference (%)")
plt.title("Climate Impact (P-ATR20) - Relative Difference Compared To Baseline")
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.savefig(f'results_report/climate_sensitivity_chapter/{prediction}_{prediction_2}_{weather_model}_{weather_model_2}_climate.png', format='png')
plt.show()