-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_precipitation_climatology.py
80 lines (56 loc) · 2.4 KB
/
plot_precipitation_climatology.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
import argparse
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
import cmocean
def convert_pr_units(darray):
"""Convert kg m-2 s-1 to mm day-1.
Args:
darray (xarray.DataArray): Precipitation data
"""
darray.data = darray.data * 86400
darray.attrs['units'] = 'mm/day'
return darray
def create_plot(clim, model, season, gridlines=False):
"""Plot the precipitation climatology.
Args:
clim (xarray.DataArray): Precipitation climatology data
model (str): Name of the climate model
season (str): Season
Kwargs:
gridlines (bool): Select whether to plot gridlines
"""
fig = plt.figure(figsize=[12,5])
ax = fig.add_subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
clim.sel(season=season).plot.contourf(ax=ax,
levels=np.arange(0, 13.5, 1.5),
extend='max',
transform=ccrs.PlateCarree(),
cbar_kwargs={'label': clim.units},
cmap=cmocean.cm.haline_r)
ax.coastlines()
if gridlines:
plt.gca().gridlines()
title = f'{model} precipitation climatology ({season})'
plt.title(title)
def main(inargs):
"""Run the program."""
dset = xr.open_dataset(inargs.pr_file)
clim = dset['pr'].groupby('time.season').mean('time', keep_attrs=True)
clim = convert_pr_units(clim)
create_plot(clim, dset.attrs['source_id'], inargs.season, gridlines=inargs.gridlines)
plt.savefig(inargs.output_file, dpi=200)
if __name__ == '__main__':
description='Plot the precipitation climatology.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument("pr_file", type=str, help="Precipitation data file")
parser.add_argument("season", type=str, help="Season to plot")
parser.add_argument("output_file", type=str, help="Output file name")
# parser.add_argument("season", type=str,
# choices=['DJF', 'MAM', 'JJA', 'SON'],
# help="Season to plot")
parser.add_argument("--gridlines", action="store_true", default=False,
help="Include gridlines on the plot")
args = parser.parse_args()
main(args)