Skip to content

Commit a29e8e7

Browse files
committed
release matplotlib memory
1 parent d0d7b28 commit a29e8e7

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

hypergas/folium_map.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def plot_png(self, out_epsg=3857, vmax=None, export_dir=None, pre_suffix=''):
273273
if cmap_vmax < vmin:
274274
cmap_vmax = vmin + 1
275275

276-
fig, ax = plt.subplots(subplot_kw=dict(projection=self._get_cartopy_crs_from_epsg(out_epsg)))
276+
fig, ax = plt.subplots(subplot_kw=dict(projection=self._get_cartopy_crs_from_epsg(out_epsg)),
277+
num=1, clear=True,
278+
)
277279

278280
# because we use longitude and latitude, we need to specify the transform.
279281
input_crs = self._get_cartopy_crs_from_epsg(4326)
@@ -307,6 +309,8 @@ def plot_png(self, out_epsg=3857, vmax=None, export_dir=None, pre_suffix=''):
307309
self.img_bounds = [[extent_4326[2], extent_4326[0]], [extent_4326[3], extent_4326[1]]]
308310

309311
# clean vars
312+
fig.clear()
313+
plt.close(fig)
310314
del da_ortho, fig, ax
311315
gc.collect()
312316

scripts/l2b_plot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,9 @@ def main(chunk=8, skip_exist=True, plot_markers=False, bbox=None):
269269
# whether plot pre-saved markers on map
270270
plot_markers = False
271271

272-
273272
# the chunk of files for each html file
274273
# don't set it too high if you meet RAM error
275-
chunk = 3
274+
chunk = 5
276275

277276
# pad (degree) around the specific location
278277
# if it is None, we will plot the whole scene

scripts/l3b_plot.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from matplotlib.colors import ListedColormap
3636

3737
from utils import get_dirs
38+
from l2b_plot import read_markers
3839

3940
warnings.filterwarnings("ignore")
4041

@@ -186,12 +187,12 @@ def sron_ime(fig, ax, ds_all, ds, df, gas, proj, plot_minimal, pad=None):
186187

187188
title = df['datetime'].item().replace('T', ' ') + '\n' \
188189
+ 'Lat: ' + str(df['plume_latitude'].round(4).item()) + ' Lon: ' + str(df['plume_longitude'].round(4).item()) + '\n' \
189-
+ str(round(df['emission'].item()/1e3, 2)) + ' t/h $\pm$ ' \
190-
+ str(round(df['emission_uncertainty']/df['emission']*100, 2).item()) + '%'
190+
+ 'IME: '+ str(round(df['emission'].item()/1e3, 1)) + ' t/h $\pm$ ' \
191+
+ str(round(df['emission_uncertainty']/df['emission']*100, 1).item()) + '%'
191192

192-
# add name to title if exists
193-
if not df['name'].isnull().item():
194-
title = str(df['name'].item()) + '\n' + title
193+
## add name to title if exists
194+
#if not df['name'].isnull().item():
195+
# title = str(df['name'].item()) + '\n' + title
195196
ax.set_title(title, fontweight='bold')
196197

197198
return extent, vmax
@@ -219,8 +220,8 @@ def sron_csf(fig, ax_ime, ax_csf, ds_csf, df):
219220
ax_csf.axhline(y=ds_csf['emission_rate'].mean()/1e3, c='orange', linestyle='--')
220221
ax_csf.set_xlabel('CSF lines')
221222
ax_csf.set_ylabel('Emission Rate (t h$^{-1}$)', c='C0')
222-
title = str(round(df['emission_csf'].item()/1e3, 2)) + ' t/h $\pm$ ' \
223-
+ str(round(df['emission_csf_uncertainty']/df['emission_csf']*100, 2).item()) + '%'
223+
title = str(round(df['emission_csf'].item()/1e3, 1)) + ' t/h $\pm$ ' \
224+
+ str(round(df['emission_csf_uncertainty']/df['emission_csf']*100, 1).item()) + '%'
224225
ax_csf.set_title(f'CSF: {title}', fontweight='bold')
225226

226227
ds_csf.close()
@@ -252,7 +253,7 @@ def cm_ime(fig, ax, ds_all, ds, df, gas, proj, plot_minimal, extent, vmax):
252253
ax.set_title(title, fontweight='bold')
253254

254255

255-
def plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, pad):
256+
def plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, plot_markers, pad):
256257
"""Plot L3 data"""
257258
LOG.info(f'Plotting {filename}')
258259

@@ -289,7 +290,7 @@ def plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_fie
289290

290291
proj = ccrs.PlateCarree()
291292

292-
fig = plt.figure(layout='compressed')
293+
fig = plt.figure(layout='compressed', num=1, clear=True)
293294

294295
# only plot csf if CSF data is available
295296
if plot_minimal:
@@ -324,6 +325,10 @@ def plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_fie
324325
# plot SRON IME results
325326
extent, vmax = sron_ime(fig, ax_ime, ds_all, ds, df, gas, proj, plot_minimal, pad)
326327

328+
if plot_markers:
329+
df_marker = read_markers()
330+
ax_ime.scatter(df_marker['longitude'], df_marker['latitude'], color='k', linewidth=2, marker='^', fc='none', s=200)
331+
327332
# plot SRON csf results
328333
if plot_csf:
329334
# read csf file
@@ -374,11 +379,13 @@ def plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_fie
374379
LOG.info(f'Exported to {savename}')
375380
fig.savefig(savename, bbox_inches='tight', pad_inches=0, dpi=300)
376381

382+
fig.clear()
383+
plt.close(fig)
377384
del ds, df, fig
378385
gc.collect()
379386

380387

381-
def main(skip_exist=True, plot_csf=True, plot_minimal=False, plot_full_field=False, pad=None):
388+
def main(skip_exist=True, plot_csf=True, plot_minimal=False, plot_full_field=False, plot_markers=False, pad=None):
382389
# get the filname list
383390
filelist = list(chain(*[glob(os.path.join(data_dir, pattern), recursive=True) for pattern in PATTERNS]))
384391

@@ -397,9 +404,9 @@ def main(skip_exist=True, plot_csf=True, plot_minimal=False, plot_full_field=Fal
397404
if os.path.isfile(savename):
398405
LOG.info(f'{savename} exists, skip ...')
399406
else:
400-
plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, pad)
407+
plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, plot_markers, pad)
401408
else:
402-
plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, pad)
409+
plot_data(filename, savename, plot_csf, plot_cm, plot_minimal, plot_full_field, plot_markers, pad)
403410

404411

405412
if __name__ == '__main__':
@@ -426,6 +433,9 @@ def main(skip_exist=True, plot_csf=True, plot_minimal=False, plot_full_field=Fal
426433
# whether plot the full field instead of plume
427434
plot_full_field = False
428435

436+
# whether plot marker dataset
437+
plot_markers = False
438+
429439
for data_dir in lowest_dirs:
430440
LOG.info(f'Plotting data under {data_dir}')
431-
main(skip_exist, plot_csf, plot_minimal, plot_full_field, pad)
441+
main(skip_exist, plot_csf, plot_minimal, plot_full_field, plot_markers, pad)

0 commit comments

Comments
 (0)