Opening Astropy FITS file from SFTP server

孤人 提交于 2021-02-08 03:30:23

问题


I have a Python script that ssh into a remote server using Paramiko module.

The below is my script

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("host", username="McMissile")

A FITS file on a local machine is usually opened as follows:

from astropy.io import fits

hdu = fits.open('File.fits')

I was wondering how would I open a FITS file from the SFTP server machine and store it under the variable hdu in the local machine.

I cannot download the file from the server to the local machine due to the storage constraints.


回答1:


Astropy.io fits.open method accepts a file-like object in place of a file name:

name : file path, file object, file-like object or pathlib.Path object


A file-like object representing a remote file is returned by Paramiko SFTPClient.open method:

A file-like object is returned, which closely mimics the behavior of a normal Python file object, including the ability to be used as a context manager.


So this should work:

sftp_client = ssh_client.open_sftp()
with sftp_client.open('remote_filename') as remote_file:
    hdu = fits.open(remote_file)


来源:https://stackoverflow.com/questions/53718571/opening-astropy-fits-file-from-sftp-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!