使用netCDF4通过Python中的ssh读取远程NetCDF文件

使用netCDF4通过Python中的ssh读取远程NetCDF文件,python,ssh,netcdf4,Python,Ssh,Netcdf4,如何使用Python远程读取NetCDF文件 这很容易,但是如何将命令sftp\u client.open()替换为netCDF4.Dataset(),以将结果存储到变量中 在以下示例中,我正在本地和临时下载要远程读取的文件: import os import paramiko import netCDF4 remotefile = 'pathtoremotefile' localfile = 'pathtolocaltmpfile' ssh_client = paramiko.SSHCli

如何使用Python远程读取NetCDF文件

这很容易,但是如何将命令
sftp\u 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)

您可以使用本地挂载远程ssh,并将其作为本地文件打开

import os
import netCDF4
localfile = 'pathtolocalmountpoint/relativepathtofile'
try:
  ncfile = netCDF4.Dataset(localfile)