AndrewAnnex / SpiceyPy

SpiceyPy: a Pythonic Wrapper for the SPICE Toolkit.
MIT License
395 stars 84 forks source link

sincpt raises error instead of 0 #483

Closed rjolitz closed 4 months ago

rjolitz commented 4 months ago

This is an error in sincpt. Instead of returning 0s when there is no intercept, it errors out entirely. This is a problem, since the error is indistinguishable from a missing kernel. It should just return 0s, as CSPICE does.

I was attempting to determine when Mars intercepts the boresight vector of the MAVEN SEP 1 sensor. When the intercept should be 0, the following error is returned:

  File "/Users/rjolitz/Software/mavenpy/../maven_energy_flux/spiceypy_error.py", line 42, in <module>
    intercept = spiceypy.sincpt(
                ^^^^^^^^^^^^^^^^
  File "/Users/rjolitz/Software/mavenpy/python/lib/python3.11/site-packages/spiceypy/spiceypy.py", line 138, in with_errcheck
    res = f(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^
  File "/Users/rjolitz/Software/mavenpy/python/lib/python3.11/site-packages/spiceypy/spiceypy.py", line 158, in wrapper
    raise NotFoundError(
spiceypy.utils.exceptions.NotFoundError: Spice returns not found for function: sincpt

When the intercept is 1, no error is returned and the fields are correct.

Here is the code to reproduce the error. Replace the spice dir with wherever you store spice files on your computer:

import os
import spiceypy

spice_dir = <<SPICE FILE FOLDER HERE>>

# Generic kernels:
spiceypy.furnsh(os.path.join(spice_dir, 'naif/generic_kernels/pck/pck00011.tpc'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/generic_kernels/lsk/naif0011.tls'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/generic_kernels/spk/satellites/mar097.bsp'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/generic_kernels/spk/planets/de430.bsp'))

# MAVEN specific kernels:
spiceypy.furnsh(os.path.join(spice_dir, 'naif/MAVEN/kernels/fk/maven_v11.tf'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/MAVEN/kernels/ik/maven_sep_v12.ti'))

# Spacecraft clock, pointing kernel, and position kernel for time of interest
spiceypy.furnsh(os.path.join(spice_dir, 'naif/MAVEN/kernels/sclk/MVN_SCLKSCET.00122.tsc'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/MAVEN/kernels/ck/mvn_sc_rel_240318_240324_v02.bc'))
spiceypy.furnsh(os.path.join(spice_dir, 'naif/MAVEN/kernels/spk/maven_orb_rec_240101_240401_v1.bsp'))

# A known time when a SEP FOV is pointing at Mars
et_i = spiceypy.str2et("March 19, 2024 03:00:00")

# This works:
intercept = spiceypy.sincpt('Ellipsoid', 'Mars', et_i, 'IAU_MARS', 'NONE', 'MAVEN', 'MAVEN_SEP2', [-1, -10, -5])
print("This works because there is an intercept: ", intercept)

# This raises an error:
print("This raises an error:")
intercept = spiceypy.sincpt('Ellipsoid', 'Mars', et_i, 'IAU_MARS', 'NONE', 'MAVEN', 'MAVEN_SEP2', [-1, 0, 0])
AndrewAnnex commented 4 months ago

@rjolitz This is the default behavior in SpiceyPy for functions that use return flags to indicate the success of the condition (raise a NotFoundError) and is specifically a different exception for a missing kernel file. The docs describe this here: https://spiceypy.readthedocs.io/en/stable/exceptions.html#not-found-errors.

you can adjust the behavior by temporarily disabling the behavior using the spice.no_found_check() context manager like:

with spiceypy.no_found_check():
     # will not raise an error and found will be False/0
     intercept, found = spiceypy.sincpt('Ellipsoid', 'Mars', et_i, 'IAU_MARS', 'NONE', 'MAVEN', 'MAVEN_SEP2', [-1, 0, 0])

or you can disable it for all subsequent calls without needing the context manager using spiceypy.spiceypy.found_check_off()

rjolitz commented 4 months ago

Oh dear. You're right, that works. I had been adapting some code from IDL into Python and checking it in both to verify accuracy. That flag must have been set somewhere within the initialization or common blocks before the routine was called, which explains why it was returning 0s in the IDL terminal just fine. Thank you, and apologies for the errant report.

AndrewAnnex commented 4 months ago

@rjolitz great, will mark this issue as resolved.