Commit 25b2fed6 authored by Jared Rennie's avatar Jared Rennie
Browse files

changed background for conus plot, and added script that uses solely AWS as inputs

parent e56b220c
#!/usr/bin/python
#####################################################################
# plot_nclimgrid_daily.py
#
# *AUTHOR*
# Written By: Jared Rennie (@jjrennie)
#
# *DESCRIPTION*
# Takes nClimGrid daily data and plots for a given day. 2 plots
# are generated. 1 for CONUS, one for the Carolinas (NC and SC).
#
# *INPUT DATA LOCATION*
# This version of the script pulls data down from the following bucket
# AWS S3: s3://noaa-nclimgrid-daily-pds/beta/by-month/
#
# *HOW TO RUN*
# python plot_nclimgrid_daily-AWS.py 2021 07 24 tmax
#####################################################################
# Import Modules
import numpy as np
import numpy.ma as ma
import s3fs
import sys, time, datetime, math, calendar
import xarray as xr
import geopandas as gpd
import rioxarray
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature
from shapely.geometry import mapping
from metpy.plots import USCOUNTIES
# Declare User Directories
main_directory="."
dataset_directory=main_directory+'/input_nclimgrid'
shapefile_directory=main_directory+'/input_shapefile'
out_directory=main_directory+"/results_plots"
#################################################
# BEGIN PROGRAM
start=time.time()
# Read in Arguments
if len(sys.argv) < 5:
sys.exit("USAGE: <YYYY> <MM> <DD> <ELEMENT>")
year= sys.argv[1]
month= sys.argv[2]
day= sys.argv[3]
element= sys.argv[4]
# Get some Info Based on Date
day_counter=int(day)
current_date="%04i%02i%02i" % (int(year),int(month),int(day))
current_month="%04i%02i" % (int(year),int(month))
day_pos=datetime.datetime(int(year),int(month),int(day)).timetuple().tm_yday
if not calendar.isleap(int(year)) and int(month) >=3:
day_pos+=1
# Set info based on Element
if element == "tmax" or element == "tmin" or element == "tavg":
unit='°F'
if element=='tmax':
element_name='Maximum Temperature'
if element=='tmin':
element_name='Minimum Temperature'
if element=='tavg':
element_name='Average Temperature'
# Set Up Color Map
cmap='RdYlBu_r'
bounds=np.array([0,10,20,30,40,50,60,70,80,90,100,110])
if element == "prcp":
unit='inches'
element_name='Precipitation'
# Set Up Color Map
cmap='YlGnBu'
bounds=np.array([0.01,0.10,0.25,0.50,0.75,1.00,2.00,3.00,4.00], dtype='f')
# Set up some other Plotting info
dpi=300
plt.style.use('dark_background')
#################################################
# READ IN DATA FROM AWS
# Open File System
s3 = s3fs.S3FileSystem(anon=False)
# Get Specific File Based on Inputs
aws_year="%04i" % (int(year))
aws_month="%02i" % (int(month))
aws_url='s3://noaa-nclimgrid-daily-pds/beta/by-month/'+aws_year+'/'+aws_month+'/'+element+'-'+aws_year+aws_month+'-grd-prelim.nc'
# Read in Data
aws_file=s3.open(aws_url)
print('READING IN nClimGrid DATA FROM AWS: ',aws_url)
data_monthly = xr.open_dataset(aws_file)
# Get Lat/Lons
nclimgrid_lats=data_monthly.lat.values
nclimgrid_lons=data_monthly.lon.values
# Set Up Spatial Configurations of nClimGrid Data (For Clipping Purposes)
data_monthly=data_monthly.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
data_monthly=data_monthly.rio.write_crs('epsg:4326', inplace=True)
# Read in Carolinas Shapefile For Clipping Purposes
input_shapefile=shapefile_directory+'/Carolinas_Merc.shp'
geo_shapefile = gpd.read_file(input_shapefile)
projection=geo_shapefile.crs
# Clip CONUS Data By Carolinas
data_monthly_clip=data_monthly.rio.clip(geo_shapefile.geometry.apply(mapping), geo_shapefile.crs, drop=False)
# Get Data, Convert from metric
if element == 'prcp':
conus_value=(data_monthly[element].values[day_counter-1,:,:] * 0.0393701)
carolinas_value=(data_monthly_clip[element].values[day_counter-1,:,:] * 0.0393701)
else:
conus_value=(data_monthly[element].values[day_counter-1,:,:] * 1.8) + 32.
carolinas_value=(data_monthly_clip[element].values[day_counter-1,:,:] * 1.8) + 32.
#################################################
# PLOTTING CONUS
print("PLOTTING CONUS")
# Set Up Colorbar info
vmin=np.min(bounds)
vmax=np.max(bounds)
extend='both'
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256)
cm = plt.cm.ScalarMappable(cmap=cmap)
cm.set_array(np.array(conus_value))
cm.set_clim(vmin, vmax)
# Set CONUS Bounds
minLat = 22
maxLat = 50
minLon = -120
maxLon = -73
# Set Up Figure
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([minLon, maxLon, minLat, maxLat], crs=ccrs.Geodetic())
ax.set_facecolor('#4a4a4a')
# Add Boundaries
ax.add_feature(cfeature.COASTLINE,linewidth=0.5)
ax.add_feature(cfeature.BORDERS,linewidth=0.5)
ax.add_feature(cfeature.STATES,linewidth=0.5)
# Plot Data
ax.pcolormesh(nclimgrid_lons, nclimgrid_lats, conus_value,transform=ccrs.PlateCarree(),cmap=cmap,norm=norm,vmin=vmin,vmax=vmax)
# Add Colorbar
cax = fig.add_axes([0.1, -0.035, 0.8, 0.03])
cbar=plt.colorbar(cm, cax=cax,boundaries=bounds,orientation='horizontal',extend=extend,spacing='uniform')
cbar.ax.tick_params(labelsize=15)
cbar.set_label(unit,size=15)
# Annotate Info On Graphic
nclimgrid_max="%6.1f" % np.nanmax(conus_value)
nclimgrid_min="%6.1f" % np.nanmin(conus_value)
plt.annotate('Source: nClimGrid Daily\nMade By: @jjrennie',xy=(1.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
if element == "prcp":
plt.annotate('MAX: '+str(nclimgrid_max)+'"\nMIN: '+str(nclimgrid_min)+'"',xy=(0.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
else:
plt.annotate('MAX: '+str(nclimgrid_max)+'°F\nMIN: '+str(nclimgrid_min)+'°F',xy=(0.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
# Add Title and Save to File
plt.suptitle('Daily '+element_name+' for '+current_date,size=17,color='white',y=1.05)
plt.savefig(out_directory+'/nclimgrid-conus_'+element+'_'+current_date+'.png', format='png', dpi=dpi,bbox_inches='tight')
plt.clf()
plt.close()
#################################################
# PLOTTING CAROLINAS
print("PLOTTING CAROLINAS")
# Set Up Colorbar info
vmin=np.min(bounds)
vmax=np.max(bounds)
extend='both'
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256)
cm = plt.cm.ScalarMappable(cmap=cmap)
cm.set_array(np.array(carolinas_value))
cm.set_clim(vmin, vmax)
# Set Carolinas Bounds
minLat = 31.75
maxLat = 36.75
minLon = -85
maxLon = -75
# Set Up Figure
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.Mercator())
ax.set_extent([minLon, maxLon, minLat, maxLat], crs=ccrs.Geodetic())
ax.set_facecolor('#4a4a4a')
# Add Boundaries
ax.add_feature(USCOUNTIES.with_scale('500k'),linewidth=0.25,edgecolor='#4a4a4a')
car_bnds = ShapelyFeature(shpreader.Reader(input_shapefile).geometries(),ccrs.Mercator(), facecolor='none',edgecolor='black',linewidth=2)
ax.add_feature(car_bnds,zorder=9)
# Plot Data
ax.pcolormesh(nclimgrid_lons, nclimgrid_lats, carolinas_value,transform=ccrs.PlateCarree(),cmap=cmap,norm=norm,vmin=vmin,vmax=vmax)
# Add Colorbar
cax = fig.add_axes([0.1, -0.035, 0.8, 0.03])
cbar=plt.colorbar(cm, cax=cax,boundaries=bounds,orientation='horizontal',extend=extend,spacing='uniform')
cbar.ax.tick_params(labelsize=15)
cbar.set_label(unit,size=15)
# Annotate Info On Graphic
nclimgrid_max="%6.1f" % np.nanmax(carolinas_value)
nclimgrid_min="%6.1f" % np.nanmin(carolinas_value)
plt.annotate('Source: nClimGrid Daily\nMade By: @jjrennie',xy=(1.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
if element == "prcp":
plt.annotate('MAX: '+str(nclimgrid_max)+'"\nMIN: '+str(nclimgrid_min)+'"',xy=(0.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
else:
plt.annotate('MAX: '+str(nclimgrid_max)+'°F\nMIN: '+str(nclimgrid_min)+'°F',xy=(0.045, -3.51), xycoords='axes fraction', fontsize=7,color='white',horizontalalignment='right', verticalalignment='bottom')
# Add Title and Save to File
plt.suptitle('Daily '+element_name+' for '+current_date,size=17,color='white',y=1.05)
plt.savefig(out_directory+'/nclimgrid-carolinas_'+element+'_'+current_date+'.png', format='png', dpi=dpi,bbox_inches='tight')
plt.clf()
plt.close()
####################
# DONE
####################
print("DONE!")
end=time.time()
print("Runtime: %8.1f seconds." % (end-start))
sys.exit()
\ No newline at end of file
......@@ -150,7 +150,7 @@ maxLon = -73
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([minLon, maxLon, minLat, maxLat], crs=ccrs.Geodetic())
ax.set_facecolor('black')
ax.set_facecolor('#4a4a4a')
# Add Boundaries
ax.add_feature(cfeature.COASTLINE,linewidth=0.5)
......
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