-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_precipitation_climatology.py
executable file
·116 lines (81 loc) · 3.56 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
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
import argparse
import pdb
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import cmocean
def apply_mask(darray, sftlf_file, realm):
"""Mask ocean or land using a sftlf (land surface fraction) file.
Args:
darray (xarray.DataArray): Data to mask
sftlf_file (str): Land surface fraction file
realm (str): Realm to mask
"""
dset = xr.open_dataset(sftlf_file)
assert realm in ['land', 'ocean'], """Valid realms are 'land' or 'ocean'"""
if realm == 'land':
masked_darray = darray.where(dset['sftlf'].data < 50)
else:
masked_darray = darray.where(dset['sftlf'].data > 50)
return masked_darray
def convert_pr_units(darray):
"""Convert kg m-2 s-1 to mm day-1.
Args:
darray (xarray.DataArray): Precipitation data
"""
assert darray.units == 'kg m-2 s-1', "Program assumes input units are kg m-2 s-1"
darray.data = darray.data * 86400
darray.attrs['units'] = 'mm/day'
return darray
def create_plot(clim, model, season, gridlines=False, levels=None):
"""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
levels (list): Tick marks on the colorbar
"""
if not levels:
levels = np.arange(0, 13.5, 1.5)
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=levels,
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)
if inargs.mask:
sftlf_file, realm = inargs.mask
clim = apply_mask(clim, sftlf_file, realm)
create_plot(clim, dset.attrs['source_id'], inargs.season,
gridlines=inargs.gridlines, levels=inargs.cbar_levels)
plt.savefig(inargs.output_file, dpi=200)
if __name__ == '__main__':
description='Plot the precipitation climatology for a given season.'
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("--gridlines", action="store_true", default=False,
help="Include gridlines on the plot")
parser.add_argument("--cbar_levels", type=float, nargs='*', default=None,
help='list of levels / tick marks to appear on the colorbar')
parser.add_argument("--mask", type=str, nargs=2,
metavar=('SFTLF_FILE', 'REALM'), default=None,
help="""Provide sftlf file and realm to mask ('land' or 'ocean')""")
args = parser.parse_args()
main(args)