[AstroPy] Catching an AstropyUserWarning as an exception
Ivan Valtchanov
ivvv68 at gmail.com
Thu May 9 09:58:54 EDT 2024
Thanks Derek,
Tried your suggestion with verify(option='exception') and it isn't raising
an exception, probably because the file is incomplete, but it passes the
verify() check.
So, I will try to set a warningfilter and see if I can make it raise an
exception.
Cheers,
Ivan
On Thu, 9 May 2024 at 15:39, Homeier, Derek <dhomeie at gwdg.de> wrote:
> :On 9 May 2024, at 1:44 PM, Ivan Valtchanov <ivvv68 at gmail.com> wrote:
> >
> > I am trying to figure out how to raise an exception for corrupted FITS
> files (unreliable ftp download of partial files). I have tried this
> approach but it doesn't seem to work and I could not find anything relevant
> in the docs.
> >
> > ###
> > from astropy.io import fits
> >
> > with fits.open('myfile.fits') as hdu:
> > try:
> > a = hdu.verify()
> > except AstropyUserWarning:
> > print ('Corrupt file, re-downloading")
> >
> A warning is not an exception, unless you set up a corresponding
> warningfilter.
> But setting the appropriate option in `verify` should do it the easiest
> way:
>
> try:
> a = hdu.verify(option=‘exception')
> except astropy.io.fits.verify.VerifyError:
> ...
>
> HTH
> Derek
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20240509/b0dba001/attachment.html>
More information about the AstroPy
mailing list