Skip to content

Commit 7aac80d

Browse files
Fix issue with permission error
1 parent 4aa6dee commit 7aac80d

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

stingray/utils.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,12 +2413,14 @@ def _int_sum_non_zero(array):
24132413
return sum
24142414

24152415

2416-
def fits_open_including_remote(filename, **kwargs):
2417-
"""Open a FITS file, including remote files with anonymous access if needed.
2416+
def fits_open_remote(filename, **kwargs):
2417+
"""Open a remote FITS file.
24182418
24192419
This function attempts to open a FITS file using `astropy.io.fits.open`. If a
24202420
`PermissionError` is raised and the filename appears to be a remote URL,
2421-
it retries opening the file with anonymous access using fsspec.
2421+
it retries opening the file with fsspec.
2422+
2423+
Requires the `botocore` package to be installed.
24222424
24232425
Parameters
24242426
----------
@@ -2438,11 +2440,40 @@ def fits_open_including_remote(filename, **kwargs):
24382440
If the file cannot be opened and anonymous access is not possible or fails.
24392441
24402442
"""
2443+
import botocore
24412444

24422445
try:
2446+
# This will work for local files and remote files with proper permissions
24432447
return fits.open(filename, **kwargs)
2444-
except PermissionError as e:
2448+
except (PermissionError, botocore.exceptions.NoCredentialsError) as e:
24452449
if "://" in filename:
2446-
logger.info(f"Permission denied for {filename}, trying anonymous access.")
2447-
return fits.open(filename, fsspec_kwargs={"anon": True}, use_fsspec=True, **kwargs)
2450+
logger.info(f"Permission denied for {filename}, trying with fsspec.")
2451+
return fits.open(filename, use_fsspec=True, fsspec_kwargs={"anon": True}, **kwargs)
24482452
raise e
2453+
2454+
2455+
def fits_open_including_remote(filename, **kwargs):
2456+
"""Open a FITS file, including remote files with anonymous access if needed.
2457+
2458+
If the filename appears to be a remote URL, it calls `fits_open_remote` to handle
2459+
potential permission issues. Otherwise, it opens the file directly with
2460+
`astropy.io.fits.open`.
2461+
2462+
Parameters
2463+
----------
2464+
filename : str
2465+
The path or URL to the FITS file to open. Can be a local file path or a remote URL.
2466+
**kwargs
2467+
Additional keyword arguments passed to `astropy.io.fits.open`.
2468+
2469+
Returns
2470+
-------
2471+
hdulist : astropy.io.fits.HDUList
2472+
The opened FITS file as an HDUList object.
2473+
2474+
"""
2475+
2476+
if "://" in filename:
2477+
return fits_open_remote(filename, **kwargs)
2478+
2479+
return fits.open(filename, **kwargs)

0 commit comments

Comments
 (0)