我试图使用Level2File来绘制2002年2月的数据,我得到了一些不一致的错误。
有时我在调用Level2File时遇到错误,这取决于我试图绘制的。z文件。有时是TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'
。有时是OSError: [Errno 22] Invalid argument
。
有时会抛出警告Unable to read volume header. Attempting to read messages.
后面跟着一吨Unknown message:
和一个数字,最后以Remaining buffered message segments for message type(s): 15 (1)
然后在调用Level2File时出错
有时错误出现在elv_angle = raddata.sweeps[sweep][0][0].el_angle
行,在这种情况下,它是IndexError: list index out of range
。
我刚刚升级到metpy 1.1.0,并在Win10上运行Python 3.9。
马上注意:我注意到所需的II级文件的扩展从2002年2月还是z(而我认为近期数据的扩展. gz还是.ar2v ?)。有没有可能这些文件太旧了,无法被Level2File处理?
下面是我正在运行的代码的简化版本(尽管它基本上只是复制了&(从Github上的示例代码粘贴)。
谢谢!
import matplotlib as mpl
mpl.use('Agg')
from metpy.io import Level2File
from metpy.plots import colortables
from matplotlib.cm import get_cmap
import numpy as np
import os
import sys
import matplotlib.pylab as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
import datetime
import matplotlib.patheffects as PathEffects
from time import time
from datetime import datetime, timedelta
def get_lonlat(x, y, nex_lon, nex_lat):
from pyproj import Proj
p = Proj(proj='aeqd', ellps='sphere',
lon_0=nex_lon,
lat_0=nex_lat)
return p(x,y,inverse=True)
def main(argv):
sweep = 0
varname = 'REF'
raddir = "C:\Users\rconn\Documents\WORK\Cases\20020203_Lake_Effect\"
filename = "KMQT20020203_200100.Z"
raddata = Level2File(raddir+filename)
elv_angle = raddata.sweeps[sweep][0][0].el_angle
# Pull data out of the file
radtime = raddata.dt
timestr = str(radtime)
timeint = int(timestr[0:4]+timestr[5:7]+timestr[8:10]+timestr[11:13]+timestr[14:16])
# First item in ray is header, which has azimuth angle
az = np.array([ray[0].az_angle for ray in raddata.sweeps[sweep]])
# 5th item is a dict mapping a var name (byte string) to a tuple
# Plotting options that change based on desired variable
if varname=='REF':
var_hdr = raddata.sweeps[sweep][0][4][b'REF'][0]
var_range = np.arange(var_hdr.num_gates) * var_hdr.gate_width + var_hdr.first_gate
var = np.array([ray[4][b'REF'][1] for ray in raddata.sweeps[sweep]])
# Get center lat, lon for plotting
lat_0 = raddata.sweeps[0][0][1].lat
lon_0 = raddata.sweeps[0][0][1].lon
lat_c = lat_0 # For plot extent
lon_c = lon_0
deltalat = 1.8
latsouth = lat_c - deltalat/2.
latnorth = lat_c + deltalat/2.
deltalon = ((5./4.)*deltalat)/np.cos(np.deg2rad(lat_0))
lonwest = lon_c - deltalon/2.
loneast = lon_c + deltalon/2.
# Turn into an array, then mask
data = np.ma.array(var)
data[np.isnan(data)] = np.ma.masked
# Convert az,range to x,y
xlocs = var_range * np.sin(np.deg2rad(az[:, np.newaxis])) * 1000
ylocs = var_range * np.cos(np.deg2rad(az[:, np.newaxis])) * 1000
lon, lat = get_lonlat(xlocs, ylocs, lon_0, lat_0)
# Plot the data
fig = plt.figure(5, figsize=(10,8))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax.outline_patch.set_linewidth(1.)
ax.outline_patch.set_zorder(6)
# Plot the data
p = ax.pcolormesh(lon, lat, data, cmap=get_cmap('viridis'), vmin=-20, vmax=80, zorder=3)
# Set custom extent for map plot
ax.set_extent([lonwest,loneast,latsouth,latnorth],ccrs.PlateCarree())
if __name__ == '__main__':
main(sys.argv[1:])
问题是.Z
文件是使用古老的UNIXcompress
程序压缩的,Python在标准库中没有任何对其解压缩的支持。这就是MetPy不支持它的原因。
最好的办法是使用gzip
(命令行实用程序可以处理.Z
)重新压缩这些文件:
for f in *.Z; do
gunzip -c $f | gzip > "${f%.Z}.gz"
done;