Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update isd_generate.py to support optional function to ensure that instrument pointing quaternions do not change sign #612

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions ale/isd_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def main():
help="Generate nadir spice pointing, an isd that has pointing directly towards "
"the center of the target body."
)
parser.add_argument(
"-f", "--fix_quaternion",
action="store_true",
help="There are rare cases when a stream of quaternion flip their signs. This option will "
"fix those to be consistent. This takes after a NASA ASP function to do the same. "
)
Comment on lines +92 to +97
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there cases where you wouldn't want this on?

Copy link
Contributor Author

@thareUSGS thareUSGS Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think @oleg-alexandrov would state this should always be run to fix for all images (that assumes I am doing this correctly). But perhaps we have a flag that turns this function off instead of on (implying we would always run it unless the flag stops it).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weighing in here with a +1 to @oleg-alexandrov's comment below. Always on.

parser.add_argument(
'--version',
action='version',
Expand Down Expand Up @@ -119,7 +125,8 @@ def main():

if len(args.input) == 1:
try:
file_to_isd(args.input[0], args.out, kernels=k, log_level=log_level, compress=args.compress, only_isis_spice=args.only_isis_spice, only_naif_spice=args.only_naif_spice, local=args.local)
file_to_isd(args.input[0], args.out, kernels=k, log_level=log_level, compress=args.compress, only_isis_spice=args.only_isis_spice,
only_naif_spice=args.only_naif_spice, local=args.local, fix_quaternion=args.fix_quaternion)
except Exception as err:
# Seriously, this just throws a generic Exception?
sys.exit(f"File {args.input[0]}: {err}")
Expand Down Expand Up @@ -157,7 +164,8 @@ def file_to_isd(
only_isis_spice=False,
only_naif_spice=False,
local=False,
nadir=False
nadir=False,
fix_quaternion=False
):
"""
Returns nothing, but acts as a thin wrapper to take the *file* and generate
Expand Down Expand Up @@ -196,6 +204,9 @@ def file_to_isd(
else:
usgscsm_str = ale.loads(file, props=props, verbose=log_level>logging.INFO, only_isis_spice=only_isis_spice, only_naif_spice=only_naif_spice)

if fix_quaternion:
usgscsm_str = fix_quaternion_signs(usgscsm_str)

if compress:
logger.info(f"Writing: {os.path.splitext(isd_file)[0] + '.br'}")
compress_json(usgscsm_str, os.path.splitext(isd_file)[0] + '.br')
Expand Down Expand Up @@ -252,8 +263,50 @@ def decompress_json(compressed_json_file):

return os.path.splitext(compressed_json_file)[0] + '.json'

def fix_quaternion_signs(usgscsm_str):
"""
Ensure that instrument pointing quaternions do not change sign

Parameters
----------
usgscsm_str : str

Returns
-------
str
usgscsm_str
"""

usgscsm_json = json.loads(usgscsm_str)
quats = usgscsm_json["instrument_pointing"]["quaternions"]

# First find the largest magnitude quaternion coefficient and its coordinate
num_quats = len(quats)
max_q = 0.0
max_j = 0
for i in range(num_quats):
for j in range(4):
if abs(quats[i][j]) > abs(max_q):
max_q = quats[i][j]
max_j = j

# Ensure the signs are consistent
qcnt = 0
for i in range(num_quats):
if quats[i][max_j] * max_q < 0:
qcnt = qcnt + 1
for j in range(4):
quats[i][j] *= -1.0

usgscsm_json["instrument_pointing"]["quaternions"] = quats
usgscsm_str = json.dumps(usgscsm_json, indent=2)
logger.info("updated {} of {} quaternions that have a sign change".format(qcnt, num_quats))
return usgscsm_str


if __name__ == "__main__":
try:
sys.exit(main())
except ValueError as err:
sys.exit(err)