-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotPotentialIntensity.py
140 lines (119 loc) · 4.86 KB
/
plotPotentialIntensity.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
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import logging
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
import cartopy.feature as feature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import shapely.geometry as sg
from netCDF4 import Dataset
from cftime import num2date
import numpy as np
from datetime import datetime
import seaborn as sns
from git import Repo
r = Repo('')
commit = str(r.commit('HEAD'))
LOGGER = logging.getLogger()
logging.basicConfig(level='INFO',
format="%(asctime)s: %(funcName)s: %(message)s",
filename='plotPI.log', filemode='w',
datefmt="%Y-%m-%d %H:%M:%S")
console = logging.StreamHandler(sys.stdout)
console.setLevel(getattr(logging, 'INFO'))
formatter = logging.Formatter('%(asctime)s: %(funcName)s: %(message)s',
datefmt='%H:%M:%S', )
console.setFormatter(formatter)
LOGGER.addHandler(console)
LOGGER.info(f"Started {sys.argv[0]} (pid {os.getpid()})")
LOGGER.info(f"Code version: f{commit}")
sns.set_context("talk")
palette = [(1.000, 1.000, 1.000), (0.000, 0.627, 0.235), (0.412, 0.627, 0.235),
(0.663, 0.780, 0.282), (0.957, 0.812, 0.000), (0.925, 0.643, 0.016),
(0.835, 0.314, 0.118), (0.780, 0.086, 0.118)]
cmap = sns.blend_palette(palette, as_cmap=True)
dataPath = "C:/WorkSpace/data/pi"
monmean = os.path.join(dataPath, "pcmin.1979-2020.nc")
ncobj = Dataset(monmean, 'r')
lat = ncobj.variables['latitude'][:]
lon = ncobj.variables['longitude'][:]
nctimes = ncobj.variables['time']
n2t = np.vectorize(num2date, excluded=['units', 'calendar'])
dts = n2t(nctimes[:], units=nctimes.units,
calendar=nctimes.calendar)
xx, yy = np.meshgrid(lon, lat)
for tdx, dt in enumerate(dts):
LOGGER.info(f"Plotting {dt.strftime('%B %Y')}")
vmax = ncobj.variables['vmax'][tdx, :, :]
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
ax.coastlines(resolution='10m', color='k', linewidth=1)
ax.add_feature(feature.BORDERS)
ax.add_feature(feature.LAND, color='beige')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=0.5, linestyle='--')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlocator = mticker.MultipleLocator(10)
gl.ylocator = mticker.MultipleLocator(5)
ax.grid(True)
cf = ax.contourf(xx, yy, vmax, cmap=cmap,
levels=np.arange(5, 121, 5), extend='both')
cs = ax.contour(xx, yy, vmax, colors='k', levels=np.arange(
5, 121, 5), linewidth=1, alpha=0.5)
ax.set_ylim((-50, 0))
ax.set_xlim((80, 180))
plt.colorbar(cf, label='Potential intensity (m/s)',
extend='max', orientation='horizontal',
shrink=0.75, aspect=30, pad=0.055)
ax.set_title(dt.strftime("%B %Y"))
plt.savefig(os.path.join(dataPath, f"pcmin.{dt.strftime('%Y-%m')}.png"),
bbox_inches='tight')
plt.close(fig)
ncobj.close()
LOGGER.info("Plotting monthly long term mean maps")
monltm = os.path.join(dataPath, "pcmin.monltm.nc")
ncobj = Dataset(monltm, 'r')
lat = ncobj.variables['latitude'][:]
lon = ncobj.variables['longitude'][:]
nctimes = ncobj.variables['time']
dts = n2t(nctimes[:], units=nctimes.units,
calendar=nctimes.calendar)
xx, yy = np.meshgrid(lon, lat)
for tdx, dt in enumerate(dts):
LOGGER.info(f"Plotting {dt.strftime('%B')}")
vmax = ncobj.variables['vmax'][tdx, :, :]
fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
ax.coastlines(resolution='10m', color='k', linewidth=1)
ax.add_feature(feature.BORDERS)
ax.add_feature(feature.LAND, color='beige')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=0.5, linestyle='--')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlocator = mticker.MultipleLocator(10)
gl.ylocator = mticker.MultipleLocator(5)
ax.grid(True)
cf = ax.contourf(xx, yy, vmax, cmap=cmap,
levels=np.arange(5, 121, 5), extend='both')
cs = ax.contour(xx, yy, vmax, colors='k', levels=np.arange(
5, 121, 5), linewidth=1, alpha=0.5)
ax.set_ylim((-50, 0))
ax.set_xlim((80, 180))
plt.colorbar(cf, label='Potential intensity (m/s)', extend='max',
orientation='horizontal',
shrink=0.75, aspect=30, pad=0.055)
ax.set_title(f"Mean potential intensity - {dt.strftime('%B')}")
plt.savefig(os.path.join(
dataPath, f"pcmin.{dt.strftime('%m')}.png"), bbox_inches='tight')
plt.close(fig)
ncobj.close()
LOGGER.info("Finished")