Commit 78d192a8 authored by Jared Rennie's avatar Jared Rennie
Browse files

Initial commit

parents
#!/usr/bin/python
###############################################################################
# get_era5-monthly.py
# Written by Jared Rennie, NCICS
# E-mail: jared@cicsnc.org
#
# Description: Pulls down ERA5 Monthly Averaged Data for a particular month
# Between 1981-2020
###############################################################################
# Import Packages
import sys
import time
import cdsapi
# User defined directories
main_directory="" #User edit
out_directory=main_directory+"/input_month"
# Begin Program
start=time.time()
# Read in Arguments
if len(sys.argv) < 3:
sys.exit("USAGE: <ELEMENT> <MONTH>")
element= sys.argv[1]
month="%02i" % int(sys.argv[2])
print(element)
print(month)
outfile="%s/era5M-%s-1981to2020-%s.nc" % (out_directory,element,month)
print(outfile)
# Use CDS API to grab
c = cdsapi.Client()
c.retrieve(
'reanalysis-era5-single-levels-monthly-means',
{
'format': 'netcdf',
'variable': str(element),
'product_type': 'monthly_averaged_reanalysis',
'year': [
'1981', '1982', '1983',
'1984', '1985', '1986',
'1987', '1988', '1989',
'1990', '1991', '1992',
'1993', '1994', '1995',
'1996', '1997', '1998',
'1999', '2000', '2001',
'2002', '2003', '2004',
'2005', '2006', '2007',
'2008', '2009', '2010',
'2011', '2012', '2013',
'2014', '2015', '2016',
'2017', '2018', '2019',
'2020',
],
'month': str(month),
'time': '00:00',
},
outfile)
####################
# DONE
####################
print("DONE!")
end=time.time()
print ("Runtime: %8.1f seconds." % (end-start))
\ No newline at end of file
###############################################################################
# plot-diff.py
# Written by Jared Rennie, NCICS
# E-mail: jared@cicsnc.org
#
# Description: Given a particular month, plot 1991-2020 minus 1981-2010 average
###############################################################################
# Import Packages
import sys
import time
import numpy as np
import numpy.ma as ma
import datetime
from calendar import monthrange
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import netCDF4 as nc
# User defined directories
main_directory="" #User edit
out_directory=main_directory+'/results_plots'
# Define other variables
year_start=1981
year_end=2020
#################################################
# BEGIN PROGRAM
start=time.time()
# Read in Arguments
if len(sys.argv) < 2:
sys.exit("USAGE: <MONTH>")
month="%02i" % int(sys.argv[1])
month_name='February'
element='t2m' # 2m_Temperature
#################################################
# READ IN DATA (ERA5)
print("\nREAD IN DATA (ERA5)")
infile = main_directory+'/input_month/era5M-2m_temperature-1981to2020-'+str(month)+'.nc'
print(infile)
era5_data = nc.Dataset(infile, mode='r')
lats = era5_data.variables['latitude'][:]
lons = era5_data.variables['longitude'][:]
time_value = era5_data.variables['time'][:]
time_units=era5_data.variables['time'].units
time_calendar=era5_data.variables['time'].calendar
data_era5=era5_data.variables[element][:,0,:,:] #ERA5 (1981-2020)
data_era5T=era5_data.variables[element][:,1,:,:] #ERA5T (2020)
data_era5[39,:,:]=data_era5T[39,:,:] # MERGE ERA5 and ERA5T
data_era5=data_era5 - 273.15 # convert K to C
data_era5=(data_era5*1.8)+32 # convert C to F
# Get Averages (1981-2010)
normals_start=1981 - year_start
normals_end=2010 - year_start
era5_normal1=np.average(data_era5[normals_start:normals_end+1,:,:],axis=0)
# Get Averages (1991-2020)
normals_start=1991 - year_start
normals_end=2020 - year_start
era5_normal2=np.average(data_era5[normals_start:normals_end+1,:,:],axis=0)
# Get Difference (1991-2020 minus 1981-2010)
era5_diff=era5_normal2 - era5_normal1
################################################
# Plot (ERA5, 1981-2010)
print("\nPLOT (ERA5, 1981-2010)")
outfile = out_directory+'/era5-2m_temperature-1981to2010-'+str(month)+'.png'
print(outfile)
# Plot
dpi=300
unit='$^\circ$F'
color_map='RdBu_r' #TEMP
bounds=np.array([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95])
vmin=np.min(bounds)
vmax=np.max(bounds)
extend='both'
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256)
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.Robinson())
plt.style.use("dark_background")
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.5)
ax.add_feature(cfeature.BORDERS,linewidth=0.5)
ax.add_feature(cfeature.STATES,linewidth=0.5)
ax.pcolormesh(lons, lats, era5_normal1,transform=ccrs.PlateCarree(),cmap=color_map,norm=norm,vmin=vmin,vmax=vmax)
# Add Colormap
cm = plt.cm.ScalarMappable(cmap=color_map)
cm.set_array(era5_normal1)
cm.set_clim(vmin, vmax)
cax = fig.add_axes([0.1, 0.005, 0.8, 0.03])
cbar=plt.colorbar(cm, cax=cax,boundaries=bounds,orientation='horizontal',extend=extend,cmap=color_map,norm=norm,spacing='uniform')
cbar.ax.tick_params(labelsize=10)
plt.title("ERA5 2m Temperature, 1981-2010 Average, "+str(month_name))
plt.savefig(outfile,bbox_inches='tight')
plt.clf()
################################################
# Plot (ERA5, 1991-2020)
print("\nPLOT (ERA5, 1991-2020)")
outfile = out_directory+'/era5-2m_temperature-1991to2020-'+str(month)+'.png'
print(outfile)
# Plot
dpi=300
unit='$^\circ$F'
color_map='RdBu_r' #TEMP
bounds=np.array([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95])
vmin=np.min(bounds)
vmax=np.max(bounds)
extend='both'
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256)
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.Robinson())
plt.style.use("dark_background")
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.5)
ax.add_feature(cfeature.BORDERS,linewidth=0.5)
ax.add_feature(cfeature.STATES,linewidth=0.5)
ax.pcolormesh(lons, lats, era5_normal2,transform=ccrs.PlateCarree(),cmap=color_map,norm=norm,vmin=vmin,vmax=vmax)
# Add Colormap
cm = plt.cm.ScalarMappable(cmap=color_map)
cm.set_array(era5_normal2)
cm.set_clim(vmin, vmax)
cax = fig.add_axes([0.1, 0.005, 0.8, 0.03])
cbar=plt.colorbar(cm, cax=cax,boundaries=bounds,orientation='horizontal',extend=extend,cmap=color_map,norm=norm,spacing='uniform')
cbar.ax.tick_params(labelsize=10)
plt.title("ERA5 2m Temperature, 1991-2020 Average, "+str(month_name))
plt.savefig(outfile,bbox_inches='tight')
plt.clf()
################################################
# Plot (ERA5, 1991-2020)
print("\nPLOT (ERA5, 1991-2020 MINUS 1981-2010)")
outfile = out_directory+'/era5-2m_temperature-DIFF-'+str(month)+'.png'
print(outfile)
# Plot
dpi=300
unit='$^\circ$F'
color_map='RdBu_r' #TEMP
bounds=np.array([-5,-4,-3,-2,-1,0,1,2,3,4,5])
vmin=np.min(bounds)
vmax=np.max(bounds)
extend='both'
norm = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256)
fig= plt.figure(num=1, figsize=(8,5), dpi=dpi, facecolor='w', edgecolor='k')
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.Robinson())
plt.style.use("dark_background")
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,linewidth=0.5)
ax.add_feature(cfeature.BORDERS,linewidth=0.5)
ax.add_feature(cfeature.STATES,linewidth=0.5)
ax.pcolormesh(lons, lats, era5_diff,transform=ccrs.PlateCarree(),cmap=color_map,norm=norm,vmin=vmin,vmax=vmax)
# Add Colormap
cm = plt.cm.ScalarMappable(cmap=color_map)
cm.set_array(era5_diff)
cm.set_clim(vmin, vmax)
cax = fig.add_axes([0.1, 0.005, 0.8, 0.03])
cbar=plt.colorbar(cm, cax=cax,boundaries=bounds,orientation='horizontal',extend=extend,cmap=color_map,norm=norm,spacing='uniform')
cbar.ax.tick_params(labelsize=10)
plt.title("ERA5 2m Temperature, 1991-2020 Minus 1981-2010, "+str(month_name)) #TEMP
plt.savefig(outfile,bbox_inches='tight') #TEMP
plt.clf()
####################
# DONE
####################
print("DONE!")
end=time.time()
print(("Runtime: %8.1f seconds" % (end-start)))
sys.exit()
Supports Markdown
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