2021-08-03

读取nc文件并输出为xyz格式的txt数据,可以被surfer等软件读取。

import numpy as np
import pandas as pd
import netCDF4 as nc

pre = “h_2n2_tpxo9_atlas_30_v3”
filename = pre +".nc"
f = nc.Dataset(filename) #读取nc文件
‘’‘读取nc数据’’’
vars = f.variables
‘’‘读取横纵坐标为网格数据’’’
x = np.array(f.variables[‘lon_z’]).reshape(-1,1)
y = np.array(f.variables[‘lat_z’]).reshape(-1,1)
xx,yy = np.meshgrid(x,y)
print(xx[:,0].shape)
grid_xy = np.hstack([xx.reshape(-1,1),yy.reshape(-1,1)])
print(grid_xy.shape)

for key in vars:
if key == “hIm” or key == “hRe”: #自己查看vars内需要什么变量
value = np.array(f.variables[key][:]).T.reshape(-1,1)
# print(value.shape)
grid_xyz = np.hstack([grid_xy,value])
# print(grid_xyz.shape)
file_name = pre+"_"+ str(key) +".txt"
np.savetxt(file_name,grid_xyz)

上一篇:【NC】单表档案参照


下一篇:【Linux】 Centos7 nc探测端口命令并实时探测