我正在尝试远程读取 netcdf 文件。
我使用 Paramiko 包来读取我的文件,如下所示:
import paramiko
from netCDF4 import Dataset
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=’hostname’, username=’usrname’, password=’mypassword’)
sftp_client = client.open_sftp()
ncfile = sftp_client.open('mynetCDFfile')
b_ncfile = ncfile.read() # ****
nc = Dataset('test.nc', memory=b_ncfile)
Run Code Online (Sandbox Code Playgroud)
但是运行速度ncfile.read()非常慢。
所以我的问题是:有没有其他方法可以远程读取 netcdf 文件,或者有什么方法可以加快速度paramiko.sftp_file.SFTPFile.read()?
我的代码首先将行写入 CSV 中io.StringIO():
fileBuffer = io.StringIO()
# write header
header_writer = csv.DictWriter(fileBuffer, fieldnames=columnNames)
header_writer.writeheader()
# write lines
writer = csv.writer(fileBuffer, delimiter=',')
for line in data:
line_dec = line.decode('ISO-8859-1')
# print([line_dec])
writer.writerow([line_dec])
Run Code Online (Sandbox Code Playgroud)
以下代码还打印所有预期行:
$print(fileBuffer.getvalue()) # -> prints all expected rows
Run Code Online (Sandbox Code Playgroud)
我还可以使用 pysftp 成功连接到 SFTP 服务器,甚至在使用 pysftp 时,代码也成功返回所有预期行:
with pysftp.Connection(host, username=user, password=pw, cnopts=cnopts) as sftp:
print('sucessfully connected to {} via Port 22'.format(host))
print(fileBuffer.getvalue()) # -> prints all expected rows
sftp.putfo(fileBuffer, file2BeSavedAs) # -> no rows put on FTP Server
Run Code Online (Sandbox Code Playgroud)
实际问题来了: …