簡體   English   中英

底圖風標載入netCDF文件

[英]Basemap windbarbs loading netCDF files

我正在嘗試使用底圖在地圖上繪制風鈎。 我有兩個netCDF文件,而我在上一個作業中使用的示例使用壓縮的numpy文件。 有沒有辦法將netCDF轉換為numpy? 還是我只是定期壓縮文件? 我的錯誤是未正確讀取文件。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from IPython.display import Image
from IPython.core.display import HTML 
plt.clf()
#Initiate a Basemap instance with the Lambert Conformal Conic projection and the specified bounds and resolution arguments.  Draw states, coastlines, and countries on the map. 
m = Basemap  (projection='lcc',lon_0=-92,lat_0=38,llcrnrlat=23.5,urcrnrlat=50,llcrnrlon=-107,urcrnrlon=-71,resolution='l',area_thresh=1000)  #Makes map window
V =np.load('042711_V.nc') #Load uandv.npz, a zipped NumPy file
U =np.load('042711_U.nc') #Load uandv.npz, a zipped NumPy file
x =np.transpose(V['x']) #Load x locations from zipped NumPy array
y =np.transpose(V['y']) #Load y locations from zipped NumPy array
u =np.transpose(V['u']) #Load u-component of velocity from zipped NumPy array
v =np.transpose(V['v']) #Load v-component of velocity from zipped NumPy array
cs=plt.contour(h, levels = range(5400,6000,60), colors = 'black') #Create contour map
plt.clabel(cs,fmt= '%.0f', inline = True) #Add labels
speed = np.sqrt(u**2+v**2) #Create speeds array
plt.barbs(x,y,u,v,speed) #Plot the wind barbs
plt.title('Brian\'s First Basemap')
m.drawcoastlines(color='black') #Draw coastlines
m.drawstates(color='green') #Draw States
m.drawcountries(color='gray') #Draw Countries
parallels = np.arange(0.,90,5.) #Create latitude reference lines every 5°
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10) #Draw latitude lines on map with preferences
meridians = np.arange(180.,360.,5.) #Create longitude reference lines every 5°
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10) #Draw longitude on map with preferences
plt.show()
#plt.savefig('Lab7_fig2.png', format='png') #Save your figure.  You should get the figure below. 

#Image(url= "Lab7_fig2.png")

下面的示例說明了如何1)讀取netCDF文件,2)將其另存為壓縮的Numpy數組,以及3)如何讀回這種壓縮的數組。

import numpy as np
import netCDF4 as nc4

# Load example data:
nc = nc4.Dataset('drycblles.default.0000000.nc')
u = nc.variables["u"][:,:]

print(type(u), u.shape)

# Save array in .npz format:
np.savez('u.npz', u)

# Read the file back:
u_npz = np.load('u.npz')

# Get list of available arrays:
keys = u_npz.files
# Read the data (there is only one array):
u_2 = u_npz[keys[0]]

print(type(u_2), u_2.shape)

<class 'numpy.ndarray'> (8, 32)

<class 'numpy.ndarray'> (8, 32)

問題可能出在如何保存壓縮的Numpy數組上。 與例如

np.savez('u.npz', u)

Numpy會自動分配名稱(鍵) arr_0arr_1等:

u_npz = np.load('u.npz')    
keys = u_npz.files
print(keys)

['arr_0']

可以為要保存的陣列提供名稱,例如:

np.savez('u.npz', u_component=u)

print(keys)將為您提供:

['u_component']

因此,根據用於保存.npz文件的方法,您必須將其讀取為:

u_2 = u_npz['arr_0']

要么

u_2 = u_npz['u_component']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM