如何使用Python远程读取NetCDF文件?
read a file from a server with ssh using Python很容易,但是如何用sftp_client.open()
之类的命令将命令netCDF4.Dataset()
替换为将结果存储到变量中?
在下面的示例中,我正在本地下载并暂时下载我想远程读取的文件:
import os
import paramiko
import netCDF4
remotefile = 'pathtoremotefile'
localfile = 'pathtolocaltmpfile'
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('myserver', username="myname", password="mypswd")
sftp_client = ssh_client.open_sftp()
# Something similar than this but for a NetCDF file?
# Or how to use remote_file in netCDF4 afterwards?
# remote_file = sftp_client.open(remotefile)
# Here, I just download it to manipulate it locally...
sftp_client.get(remotefile, localfile)
try:
ncfile = netCDF4.Dataset(localfile)
# Operations...
finally:
sftp_client.close()
ssh_client.close()
os.remove(localfile)
答案 0 :(得分:0)
您可以使用sshfs在本地安装远程ssh并将其作为本地文件打开
import os
import netCDF4
localfile = 'pathtolocalmountpoint/relativepathtofile'
try:
ncfile = netCDF4.Dataset(localfile)