Commit 45d74fb1 authored by Carl Schreck's avatar Carl Schreck

Working version of prcp_case.py

Also added date labels and legend to ibtracs.py
parent df44e22e
......@@ -28,7 +28,7 @@ if month < 1:
month += 12
year -= 1
first_days = np.array([1, 7, 12, 19, 28])
first_days = np.array([1, 10, 20])
days_in_month = calendar.monthrange(year, month)[1]
clobber = False
......
......@@ -19,7 +19,7 @@ cjs.tstamp('Here we go!')
basins = ['NA', 'EP']
# basins = ['WMOSI']
parser = argparse.ArgumentParser()
parser.add_argument('--year', default=2020)
parser.add_argument('--year', default=(datetime.today().year))
parser.add_argument('--month', default=(datetime.today().month - 1))
args = parser.parse_args()
end_year = int(args.year)
......
""" Draw the steps for one temperature variable. """
__author__ = "Carl Schreck"
__email__ = "cjschrec@ncsu.edu"
__copyright__ = "Copyright 2020, North Carolina State University"
__license__ = "BSD-3.0"
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import os
import numpy as np
import cartopy.crs as ccrs
import argparse
from datetime import datetime
import cjs # '~carl/lib/python'
import ibtracs
# These are some parameters that could be useful to have up top
parser = argparse.ArgumentParser()
parser.add_argument('--year', default=(datetime.today().year))
parser.add_argument('--month', default=(datetime.today().month - 1))
parser.add_argument('--first_day', default=26)
parser.add_argument('--last_day', default=28)
args = parser.parse_args()
year = int(args.year)
month = int(args.month)
if month < 1:
month += 12
year -= 1
first_day = int(args.first_day)
last_day = int(args.last_day)
time_range = slice(f'{year}-{month:02}-{first_day:02}',
f'{year}-{month:02}-{last_day:02}')
extent = [-102.0, -94.0, 25.5, 32.0]
# extent = [-102.0, -90.0, 25.5, 35.0]
central_longitude = np.mean(extent[0:2])
levels = [0.1, 0.5, 1, 5, 10, 25, 50, 75, 100, 150, 200]
mm_to_inches = 1 / 25.4
# mm_to_inches = 0.04
# levels = np.array([1, 5, 10, 25, 50, 75, 100, 150, ]) * mm_to_inches
levels = [0.05, 0.25, 0.5, 1, 2, 3, 4, 5, 6]
tif_name = 'MSR_50M'
cjs.tstamp('Here we go!')
file_type = 'scaled'
file_name = f'prcp-{year}{month:02}-grd-{file_type}.nc'
in_path = f'{os.environ["DATA_DIR"]}/nclimgrid/beta/prcp/{file_name}'
if not os.path.exists(in_path):
file_type = 'prelim'
file_name = f'prcp-{year}{month:02}-grd-{file_type}.nc'
in_path = f'{os.environ["DATA_DIR"]}/nclimgrid/beta/prcp/{file_name}'
cmap = cm.get_cmap('GnBu')
newcolors= cmap(np.linspace(0, 1, 256))
newcolors[0] = [1, 1, 1, 1]
precip_cmap = mcolors.ListedColormap(newcolors)
cjs.tstamp('Read and average totals')
ds = xr.open_dataset(in_path)
data = ds['prcp'].sel(time=time_range).sum(dim='time')
data *= mm_to_inches
fig, ax = plt.subplots(1, 1, subplot_kw=dict(projection=cjs.conus_projection))
cjs.add_map(ax=ax, linewidth=1.0, scale='10m')
plot = data.plot(ax=ax, transform=ccrs.PlateCarree(),
add_colorbar=False,
cmap=precip_cmap, levels=levels, zorder=1,
vmin=-np.inf, vmax=np.inf, extend='both',
)
ax.set_title(f'Tropical Storm Hanna July {first_day}-{last_day}')
ax.set_extent(extent)
cjs.add_shapes(ax=ax, shp_name='countyl010g', facecolor='none')
ax.outline_patch.set_visible(True)
ibt = ibtracs.Ibtracs(in_path='last3years', download_data=False)
ibt.map_one_storm_dots(ax=ax, sid='2020204N25274', add_date_labels=True,
zorder=99)
# cjs.add_shaded_relief(ax=ax)
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="5%", pad=0.10, axes_class=plt.Axes)
fig.add_axes(cax)
str_levels = [str(lev) for lev in levels]
cb = plt.colorbar(plot, cax=cax, orientation='horizontal',
panchor=False, label='Precipitation (inches)')
cb.set_ticks(levels)
cb.set_ticklabels(str_levels)
cjs.tstamp('Add labels')
plt.text(1.25, -0.30, 'Data Source: 5km Gridded\n'
'(nClimGrid-Daily) Beta',
fontsize=7, ha='right', transform=ax.transAxes)
today_string = (f'{datetime.now().strftime("Created: %a %b %d %Y")}\n'
f'{file_type.capitalize()} data')
plt.text(0.0, -0.30, today_string,
fontsize=7, ha='left', transform=ax.transAxes)
cjs.tstamp('Drawing figure')
# plt.tight_layout()
plt.savefig(f'figures/final/prcp_case.png', dpi=300, bbox_inches='tight')
plt.show()
cjs.tstamp('Thank you, come again.')
......@@ -11,12 +11,22 @@ from matplotlib import pyplot as plt
from matplotlib import dates as mdates
import os
import calendar
from datetime import datetime
import argparse
import cjs # '~carl/lib/python'
cjs.tstamp('Here we go!')
# These are some parameters that could be useful to have up top
month = 6
year = 2020
parser = argparse.ArgumentParser()
parser.add_argument('--year', default=(datetime.today().year))
parser.add_argument('--month', default=(datetime.today().month - 1))
args = parser.parse_args()
year = int(args.year)
month = int(args.month)
if month < 1:
month += 12
year -= 1
days_in_month = calendar.monthrange(year, month)[1]
time_range = slice(f'{year}-{month}-01', f'{year}-{month}-{days_in_month}')
clobber = True
......@@ -73,7 +83,7 @@ plt.legend(labels=[f'{x.upper()}: '
title='Monthly Values')
plt.tight_layout()
plt.savefig(f'figures/teleconnections-{year}-{month:02}.png', dpi=200)
plt.savefig(f'figures/final/teleconnections-{year}-{month:02}.png', dpi=200)
# plt.show()
plt.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment