1

How can I read a NetCDF file remotely with Python?

It is easy to read a file from a server with ssh using Python, but how can I replace the command sftp_client.open() by something like netCDF4.Dataset() to store that result into a variable?

In the following example, I am downloading locally and temporarily the file I'd like to read remotely:

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)

1 Answer 1

1

you can mount locally the remote ssh using sshfs and open it as a localfile

import os
import netCDF4
localfile = 'pathtolocalmountpoint/relativepathtofile'
try:
  ncfile = netCDF4.Dataset(localfile)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.