Commit 79a4630e authored by Carl Schreck's avatar Carl Schreck

Version 2 for temp & Precip

parent e9b38686
...@@ -8,9 +8,7 @@ __license__ = "BSD-3.0" ...@@ -8,9 +8,7 @@ __license__ = "BSD-3.0"
import xarray as xr import xarray as xr
import os import os
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs import cartopy.crs as ccrs
import cartopy.feature as cfeature
import calendar import calendar
import cartopy.io.shapereader as shapereader import cartopy.io.shapereader as shapereader
import matplotlib as mpl import matplotlib as mpl
...@@ -69,14 +67,9 @@ for shape in shapereader.Reader(state_shp_path).records(): ...@@ -69,14 +67,9 @@ for shape in shapereader.Reader(state_shp_path).records():
cjs.tstamp('Draw data') cjs.tstamp('Draw data')
levels = [5, 10, 25, 50, 75, 100, 125, 150, 200, 300, 500] levels = [5, 10, 25, 50, 75, 100, 125, 150, 200, 300, 500]
cmap = mpl.cm.get_cmap('BrBG', len(levels)+1) cmap = cjs.make_middle_white('BrBG', len(levels)+1)
mid_level = int(len(levels)/2)
for key in ('red', 'green', 'blue'):
cmap._segmentdata[key][mid_level:mid_level+1] = [(1,1,1)]
print(key)
plot = anom_data.plot(levels=levels, ax=ax, transform=ccrs.PlateCarree(), plot = anom_data.plot(levels=levels, ax=ax, transform=ccrs.PlateCarree(),
cmap='BrBG', zorder=2, cmap=cmap, zorder=2,
cbar_kwargs={'orientation': 'horizontal', cbar_kwargs={'orientation': 'horizontal',
'label': 'Percent', 'label': 'Percent',
'pad': 0.0}) 'pad': 0.0})
......
...@@ -10,7 +10,6 @@ import os ...@@ -10,7 +10,6 @@ import os
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import cartopy.crs as ccrs import cartopy.crs as ccrs
import cartopy.feature as cfeature
import calendar import calendar
import cartopy.io.shapereader as shapereader import cartopy.io.shapereader as shapereader
import matplotlib as mpl import matplotlib as mpl
...@@ -20,8 +19,8 @@ from datetime import datetime ...@@ -20,8 +19,8 @@ from datetime import datetime
# These are some parameters that could be useful to have up top # These are some parameters that could be useful to have up top
year = 2019 year = 2019
month = 8 month = 8
first_day = 31 first_day = 1
last_day = 31 last_day = 30
time_range = slice(f'{year}-{month:02}-{first_day:02}', time_range = slice(f'{year}-{month:02}-{first_day:02}',
f'{year}-{month:02}-{last_day:02}') f'{year}-{month:02}-{last_day:02}')
...@@ -47,45 +46,53 @@ clim_ds = xr.open_dataset(clim_path) ...@@ -47,45 +46,53 @@ clim_ds = xr.open_dataset(clim_path)
clim_data = clim_ds['tavg'].sel(dayofyear=dayofyear.values).mean( clim_data = clim_ds['tavg'].sel(dayofyear=dayofyear.values).mean(
dim='dayofyear') dim='dayofyear')
cjs.tstamp('Anomalize and plot') cjs.tstamp('Anomalize')
anom_data = (total_data - clim_data) * 9 / 5
cjs.tstamp('Draw map')
ax = plt.axes(projection=ccrs.AlbersEqualArea(central_longitude=360-95, ax = plt.axes(projection=ccrs.AlbersEqualArea(central_longitude=360-95,
standard_parallels=(40,60))) standard_parallels=(40,60)))
plt.gca().outline_patch.set_visible(False) plt.gca().outline_patch.set_visible(False)
ax.set_extent([-119, -75, 23, 50]) ax.set_extent([-118, -75, 23.5, 50.5])
shp_path = f'{os.environ["DATA_DIR"]}/geography/ne_50m_admin_1_states_provinces/ne_50m_admin_1_states_provinces.shp' state_shp_path = f'{os.environ["DATA_DIR"]}' \
for shape in shapereader.Reader(shp_path).records(): f'/geography/ne_10m_admin_1_states_provinces_lakes' \
f'/ne_10m_admin_1_states_provinces_lakes.shp'
for shape in shapereader.Reader(state_shp_path).records():
if shape.attributes['adm0_a3'] == 'USA': if shape.attributes['adm0_a3'] == 'USA':
ax.add_geometries(shape.geometry, ccrs.PlateCarree(), ax.add_geometries(shape.geometry, ccrs.PlateCarree(),
edgecolor='dimgrey', facecolor='none') edgecolor='none', facecolor='silver', zorder=1)
ax.add_geometries(shape.geometry, ccrs.PlateCarree(),
edgecolor=(0.2, 0.2, 0.2), linewidth=0.5,
facecolor='none', zorder=3)
anom_data = total_data - clim_data cjs.tstamp('Draw data')
anom_data = anom_data * 9 / 5 levels = list(np.arange(-5,6)*3)
levels = np.arange(-5,6)*3 cmap = cjs.make_middle_white('RdBu', len(levels)+1)
plot = anom_data.plot(levels=levels, ax=ax, transform=ccrs.PlateCarree(), plot = anom_data.plot(levels=levels, ax=ax, transform=ccrs.PlateCarree(),
cmap='RdBu', cmap=cmap, zorder=2,
cbar_kwargs={'orientation': 'horizontal', cbar_kwargs={'orientation': 'horizontal',
'label': 'Degrees Fahrenheit', 'label': 'Degrees Fahrenheit',
'pad': 0.2}) 'pad': 0.0})
plot.colorbar.set_ticklabels(levels) plot.colorbar.set_ticklabels(levels)
plot.colorbar.set_ticks(levels) plot.colorbar.set_ticks(levels)
cjs.tstamp('Add labels')
date_string = f'{calendar.month_name[month]} {first_day}-{last_day} {year}' date_string = f'{calendar.month_name[month]} {first_day}-{last_day} {year}'
plt.text(0.5, 1.11, 'Mean Temperature Departures from Average', fontsize=14, plt.text(0.5, 1.11, 'Mean Temperature Departures from Average',
ha='center', fontsize=14, ha='center', transform=ax.transAxes)
transform=ax.transAxes) plt.text(0.5, 1.05, date_string,
plt.text(0.5, 1.05, date_string, fontsize=11, ha='center', fontsize=11, ha='center', transform=ax.transAxes)
transform=ax.transAxes) plt.text(0.5, 1.00, 'Average Period: 1981-2010',
plt.text(0.5, 1.00, 'Average Period: 1981-2010', fontsize=9, ha='center', fontsize=9, ha='center', transform=ax.transAxes)
transform=ax.transAxes)
plt.text(1.1, -0.25, 'Data Source: 5km Gridded\n(nClimGrid-Daily) Beta', plt.text(1.1, -0.25, 'Data Source: 5km Gridded\n(nClimGrid-Daily) Beta',
fontsize=9, ha='right', transform=ax.transAxes) fontsize=7, ha='right', transform=ax.transAxes)
today_string = datetime.now().strftime('Created: %a %b %d %Y') today_string = datetime.now().strftime('Created: %a %b %d %Y')
plt.text(-0.1, -0.23, today_string, plt.text(-0.1, -0.23, today_string,
fontsize=9, ha='left', transform=ax.transAxes) fontsize=7, ha='left', transform=ax.transAxes)
l, b, w, h = ax.get_position().bounds cjs.tstamp('Save figure')
ax.set_position([l-(0.15*w), b-0.15, w*1.3, h*1.3]) plt.tight_layout()
plt.savefig(f'figures/tavg_{year}_{month:02}_' plt.savefig(f'figures/tavg_{year}_{month:02}_'
f'{first_day:02}-{last_day:02}.png') f'{first_day:02}-{last_day:02}.png')
......
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