Here's my attempt on a python version of plotgrids.ncl: it reads a WPS namelist and shows your domain configuration on a map.
In [124]:
# Import packages
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
In [125]:
def readnamelistwps(filename):
''' Read WPS namelist, convert to float if possible'''
# Open namelist file and read the lines
with open(filename,'r') as f:
lines = [line.strip(',\n') # remove line endings
.replace(',',' ') # remove excessive commas
.replace("'",' ') # remove excessive string quotes
.split() for line in f] # split all elements seperated by blanks into a list of items
# Create namelist dictionary
namelist = {}
for line in lines:
if len(line)>3: # Lines with namelist variables with at least four elements: name, '=', value1,value2
try:
namelist[line[0]]=[float(x) for x in line[2:]]
except ValueError:
namelist[line[0]]=line[2:]
elif len(line)>2: # Lines with namelist variables have at least three elements: name, '=', value1
try:
namelist[line[0]]=float(line[2])
except ValueError:
namelist[line[0]]=line[2]
return namelist
# Preview
namelist = readnamelistwps('namelist.wps.3nest')
namelist
Out[125]:
In [126]:
def drawouterdomain(namelist):
if namelist['max_dom']>1:
width = namelist['dx'][0]*namelist['e_we'][0]
height = namelist['dy'][0]*namelist['e_sn'][0]
else:
width = namelist['dx']*namelist['e_we']
height = namelist['dy']*namelist['e_sn']
# Create basemap instance using namelist variables
m = Basemap(width=width,height=height,
rsphere=(6378137.00,6356752.3142),\
resolution='l',area_thresh=1000.,projection='lcc',\
lat_1=float(namelist['truelat1']),lat_2=float(namelist['truelat2']),
lat_0=float(namelist['ref_lat']),lon_0=float(namelist['ref_lon']))
# Fill background
#m.fillcontinents(color='coral',lake_color='aqua')
m.etopo() # requires PIL, not on hpc
# Draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,10.))
m.drawmeridians(np.arange(-180.,181.,10.))
return m
m = drawouterdomain(namelist)
In [127]:
def drawdomains(ndom,namelist,m):
'''Given a namelist and the number of the domain, draw domain boundaries on given basemap instance'''
width = namelist['dx'][ndom]*namelist['e_we'][ndom]
height = namelist['dy'][ndom]*namelist['e_sn'][ndom]
# Central point
lon = namelist['ref_lon']
lat = namelist['ref_lat']
x,y = m(lon, lat) # convert to figure coordinates (meters)
# Define the corners of the rectangle (first and last are the same)
xs=[x-.5*width,x+.5*width,x+.5*width,x-.5*width,x-.5*width]
ys=[y-.5*height,y-.5*height,y+.5*height,y+.5*height,y-.5*height]
# Draw the contours
m.plot(xs,ys,lw=3,color='k')
return
for x in range(int(namelist['max_dom'])):
drawdomains(x,namelist,m)