StationPlot and plot_parameter errors #2011
-
This code works without the stn_plot.plot_parameter* lines. I have attached the metar source data file and here is the text for the script. It seems like I might have old version of metpy or cartopy or something, but I'm not sure how to check those versions. Or I might have something else totally wrong. Thanks for your help! import netCDF4
import os
import shutil
import numpy as np
import datetime as dt
import matplotlib
matplotlib.use('agg')
import metpy.calc as mpcalc
from metpy.units import units
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from metpy.plots import StationPlot
# initalize lists for each varible required in the surface plot
lat = []; lon = []; temp = []; dewpt = []; pres = []
wind_spd = []; wind_dir = []; ids = []
# get the current time in formatted string for 2 hour earlier
time = dt.datetime.utcnow() - dt.timedelta(hours=1)
yearmonday = time.strftime('%Y%m%d')
hour = time.strftime('%H')
# define directories for scr, and ftp
scr_dir = '/scr/ctm/catalog/tmp'
metar_dir = '/export/ldm/data/madis/metar/'
ftp_dir = '/net/iftp2/pub/incoming/catalog/test'
# get and unzip the netCDF files from the ldm directories
os.chdir(scr_dir)
madis_file = ("%s_%s00.gz" % (yearmonday,hour))
metar_file = ("%s%s_%s00.gz" % (metar_dir,yearmonday,hour))
shutil.copy2(metar_file,scr_dir)
metar_zipped = ("metar_%s" % (madis_file))
os.rename(madis_file,metar_zipped)
os.system('gunzip %s' % (metar_zipped))
# open the netCDF file and read in the required variables
metar = netCDF4.Dataset(metar_zipped[:-3])
ID = np.array(metar.variables["stationName"][:])
ID = [b''.join(name).decode('utf-8').strip() for name in ID]
latitude = np.array(metar.variables["latitude"][:])
longitude = np.array(metar.variables["longitude"][:])
temperature = np.array(metar.variables["temperature"][:])
dewpoint = np.array(metar.variables["dewpoint"][:])
pressure = np.array(metar.variables["seaLevelPress"][:])
windDir = np.array(metar.variables["windDir"][:])
windSpeed = np.array(metar.variables["windSpeed"][:])
metar.close()
# get the required variables valid for only stations in the config
for i,full_data in enumerate(zip(ID,latitude,longitude,temperature,dewpoint,pressure,\
windDir,windSpeed)):
#for id in stations:
if full_data[1] > 24 and full_data[1] < 41 and full_data[2] > -114\
and full_data[2] < -78 and full_data[3] < 330 and full_data[4] < 330\
and full_data[6] < 361 and full_data[7] < 50:
# if station name has already been found, delete the old
# timestep/data and add the more recent timestep/data
if full_data[0] in ids:
ind = ids.index(full_data[0])
del ids[ind]; del lat[ind]; del lon[ind]; del temp[ind];
del dewpt[ind]; del pres[ind]; del wind_dir[ind]; del wind_spd[ind];
print(full_data)
ids.append(full_data[0])
lat.append(full_data[1])
lon.append(full_data[2])
temp.append(full_data[3]-273.15) # convert to F
dewpt.append(full_data[4]-273.15) # convert to F
pres.append(full_data[5]/100) # convert to hPa
wind_dir.append(full_data[6])
wind_spd.append(full_data[7])
# create np.array and set values pressure to nan for missing values
lat = np.array(lat, dtype=float)
lon = np.array(lon, dtype=float)
temp = np.array(temp, dtype=float)
wind_spd = np.array(wind_spd, dtype=float)
wind_dir = np.array(wind_dir, dtype=float)
pres = np.array(pres, dtype=float)
pres[pres > 3e30] = np.nan
print(len(lat))
# create the figures with features and bounds
map_crs = ccrs.Mercator()
data_crs = ccrs.PlateCarree()
fig = plt.figure(figsize=(14,12))
ax = fig.add_subplot(1,1,1,projection=map_crs)
ax.set_extent([-114,-78,24,41], data_crs)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))
# create the station plot and plot temp, dewpt, and wind barbs
stn_plot = StationPlot(ax,lon,lat,fontsize=10,transform=data_crs)
#stn_plot.plot_parameter('NW',temp,color='red')
#stn_plot.plot_parameter('NE',pres,color='blue')
#stn_plot.plot_parameter('SW',dewpt,color='green')
(u,v) = mpcalc.wind_components((wind_spd*units.meters/units.second)\
.to('knots'),wind_dir*units.degrees)
stn_plot.plot_barb(u,v,fill_empty=True)
plt.title(('%s %s:00 UTC' % (yearmonday,hour)),fontsize=16,loc='right')
plt.title('METAR and RAWS',fontsize=16,loc='left')
# Save the figure and copy to ftp
outfile = ("surface.METAR_RAWS.%s%s00.stn_plot.png" % (yearmonday,hour))
print(outfile)
plt.savefig(outfile,bbox_inches='tight',pad_inches=0)
plt.clf()
shutil.copy(outfile,ftp_dir)
# remove the netcdf files and plot
os.remove(outfile)
os.remove(metar_zipped[:-3]) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Thanks for the code sample and data file. Unfortunately (or maybe fortunately?) it works perfectly for me with MetPy 1.0.1 and CartoPy 0.19.0.post1: Can you reply with the full text of the error traceback you get when you try to run it? Also, how did you install MetPy/Cartopy? Can you post the output of |
Beta Was this translation helpful? Give feedback.
Thanks for the code sample and data file. Unfortunately (or maybe fortunately?) it works perfectly for me with MetPy 1.0.1 and CartoPy 0.19.0.post1:
Can you reply with the full text of the error traceback you get when you try to run it? Also, how did you install MetPy/Cartopy? Can you post the output of
python -c 'import metpy; print(metpy.__version__)'
andpython -c 'import cartopy; print(cartopy.__version__)'
?