Skip to content

Instantly share code, notes, and snippets.

@GarryLai
Last active August 25, 2023 12:44
Show Gist options
  • Save GarryLai/551b3be8bd46749e7f924f05d011a1af to your computer and use it in GitHub Desktop.
Save GarryLai/551b3be8bd46749e7f924f05d011a1af to your computer and use it in GitHub Desktop.
Python讀取WISSDOM三維風場反演系統輸出格式
def wissdom_getvar(filename, nx, ny, nz, feild_id):
#filename: wissdom_out bin檔位置
#nx, ny, nz: 反演範圍大小,可參考Grads ctl檔的XDEF、YDEF、ZDEF
#feild_id: 欲讀取的參數場類別,可參考Grads ctl檔的VARS下面那一段
# 照順序下來第一個參數場為0,第二個為1,以此類推
# 常用0=u1, 1=v1, 2=w1, 3=u2, 4=v2, 5=w2
#輸出為Numpy矩陣,維度是(nz, ny, nx)
data = []
with open(filename,"rb") as f:
f.seek(4*nz*ny*nx*feild_id) #將指標偏移到欲讀取的參數場
for i in range(nz*ny*nx):
b = f.read(4) #一個float佔4個bytes
data.append(struct.unpack('<f', b)[0]) #little_endian float
data = np.array(data).reshape((nz, ny, nx))
data[data == -999] = np.nan
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment