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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值