Skip to content

Instantly share code, notes, and snippets.

@drbitboy
Created February 25, 2018 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbitboy/921fcf899ef1d7c1c631baae459e4626 to your computer and use it in GitHub Desktop.
Save drbitboy/921fcf899ef1d7c1c631baae459e4626 to your computer and use it in GitHub Desktop.
Three applications to convert Altitude and Azimuth to a NADIR_SUN-relative quaternion
/*
Purpose:
Convert nadir-relative Altitude and Azimuth to
nadir-relative quaternion
Usage:
altaz2nrq Alt Az MuNaught
Alt - Altitude = angular offset of [S/C +Z] from [NADIR_SUN +Z], degrees
Az - Azimuth => direction of offset of [S/C +Z] from [NADIR_SUN +Z], degrees
- Zero => Offset is toward [NADIR_SUN +X] i.e. toward Sun
- Increasing (positive) toward [NADIR_SUN +Y]
MuNaught - cosine of ORX<-BennuCenter->Sun angle
- Range (-1:+1)
- Zero for terminator orbit and terminator crossings
Compilation and linkage:
gcc -DDO_MAIN -Icspice/include altaz2nrq.c cspice/lib/cspice.a -lm -o altaz2nrq
*/
#include <math.h>
#include <stdio.h>
#include "SpiceUsr.h"
void
altaz2nrq(double altDeg, double azDeg, double mu0, SpiceDouble opsQuat[4]) {
SpiceDouble decRad = (90. - altDeg) * rpd_c();
SpiceDouble raRad = azDeg * rpd_c();
SpiceDouble vScZns[3];
SpiceDouble vSunns[3] = { sqrt(1.-(mu0*mu0)), 0., mu0 };
SpiceDouble spiceQuat[4];
SpiceDouble mtxNStoSC[3][3];
radrec_c(1.,raRad,decRad,vScZns);
twovec_c(vScZns,3, vSunns, 1, mtxNStoSC);
m2q_c(mtxNStoSC, spiceQuat);
vminus_c(spiceQuat+1, opsQuat+0);
opsQuat[3] = spiceQuat[0];
return;
}
#ifdef DO_MAIN
void
Usage(char* msg, char* arg) {
fprintf(stderr, "\nERROR: %s%s%s%s\n"
, msg
, arg ? " Bad argument: [" : ""
, arg ? arg : ""
, arg ? "]" : ""
);
fprintf(stderr, "\nUsage: altaz2nrq Alt Az MuNaught\n");
}
int
main(int argc, char** argv) {
double altDeg;
double azDeg;
double mu0;
SpiceDouble opsQuat[4];
if (argc != 4) {
Usage("Incorrect number of arguments", 0);
return 1;
}
if (1!=sscanf(argv[1], "%lf", &altDeg)) {
Usage("Invalid altitude argument", argv[1]);
return 1;
}
if (1!=sscanf(argv[2], "%lf", &azDeg)) {
Usage("Invalid azimuth argument", argv[2]);
return 1;
}
if (1!=sscanf(argv[3], "%lf", &mu0)) {
Usage("Invalid MuNaught argument", argv[3]);
return 1;
}
if (mu0 <= -1. || mu0 >= +1.) {
Usage("MuNaught argument out of (+1:+1) range", argv[3]);
return 1;
}
altaz2nrq(altDeg, azDeg, mu0, opsQuat);
fprintf(stdout, "[altitude=%lgdeg,azimuth=%lgdeg,muNaught=%lg] => [%.16lg, %.16lg, %.16lg, %.16lg]\n"
, altDeg, azDeg, mu0
, opsQuat[0], opsQuat[1], opsQuat[2], opsQuat[3]
);
return 0;
}
#endif /* DO_MAIN */
import spice.basic.*;
import java.util.Arrays;
////////////////////////////////////////////////////////////////////////
// Purpose:
//
// Convert nadir-relative Altitude and Azimuth to
// nadir-relative quaternion
//
// Usage:
//
// java [-cp .:spice.jar] altaz2nrq Alt Az MuNaught
//
// Alt - Altitude = angular offset of [S/C +Z] from [NADIR_SUN +Z], degrees
// Az - Azimuth => direction of offset of [S/C +Z] from [NADIR_SUN +Z], degrees
// - Zero => Offset is toward [NADIR_SUN +X] i.e. toward Sun
// - Increasing (positive) toward [NADIR_SUN +Y]
// MuNaught - cosine of ORX<-BennuCenter->Sun angle
// - Range (-1:+1)
// - Zero for terminator orbit and terminator crossings
//
// Compile: javac -cp .:spice.jar altaz2nrq.java
//
// Run: export LD_LIBRARY_PATH=/top/JNISpice/lib
// java -cp .:spice.jar altaz2nrq 11.5 115 0
//
// N.B. [-cp .:spice.jar] is not required if spice/ directory is a
// subdirectory of the current working directory
//
////////////////////////////////////////////////////////////////////////
public class altaz2nrq extends Object
{
public static double[] altaz2nrqCall (double altDeg, double azDeg, double mu0)
throws SpiceException
{
//
// Vectors are expressed in the NADIR_SUN (subscript ns) frame
// - +Zns points Nadir i.e. from S/C to center of Bennu
// - +Xns defines the half of the ZXns plane that contains the Sun
//
// - In the NS frame:
// - Azimuth is equivalent to Right Ascension
// - Altitude is equivalent to (90 - Declination)
//
// Convert angles from degrees to radians
double rpd = CSPICE.rpd();
double raRad = azDeg * rpd;
double decRad = (90. - altDeg) * rpd;
//
// Convert modified Altitude and Azimuth to vector, expressed in NADIR_SUN
// frame, where S/C +Z will point
Vector3 vScZns = new RADecCoordinates(1., raRad, decRad).toRectangular();
//
// Unit vector toward sun has Y=0 and Z=MuNaught
//
Vector3 vSunns = new Vector3(Math.sqrt(1.-(mu0*mu0)), 0., mu0);
//
// Line up S/C +Z to vScZns, and Sun vector in +X half of ZX plane
// - Result is matrix that converts a vector expressed in the NADIR_SUN
// frame to the equivalent vector expressed in the S/C frame
//
Matrix33 mtxNStoSC = new Matrix33(vScZns, 3, vSunns, 1);
//
// Convert to quaternion, and take conjugate to invert its sense
// - Alternative would be to use ntxNStoSC.xpose() (transpose, i.e.
// "mtxSCtoNS") and don't use conjugate.
//
SpiceQuaternion opsQuat = new SpiceQuaternion(mtxNStoSC).conjugate();
//
// Convert to array, reodering to put vector 1st and scalar last, and return
double[] opsArray = { opsQuat.getElt(1), opsQuat.getElt(2), opsQuat.getElt(3), opsQuat.getElt(0) };
return opsArray;
}
public static void main ( String[] args )
{
try {
// Convert command-line argument to doubls
double altDeg = Double.parseDouble(args[0]);
double azDeg = Double.parseDouble(args[1]);
double mu0 = Double.parseDouble(args[2]);
// Pass alt, az and MuNaught to altaz2nrqCall to get quaternion arrya,
// print out values in a string
System.out.println(Arrays.toString(altaz2nrqCall(altDeg,azDeg,mu0)) );
} catch ( SpiceException se) {
// SPICE exceptions
se.printStackTrace();
} catch ( Exception e) {
// Probable errors in command-line arguments; show usage
e.printStackTrace();
System.out.println("Usags: java [-Djava.system.path=/top/JNISpice/lib/] altaz2nrq altitude(degrees) azimuth(degrees) MuNaught");
}
}
//////////////////////////////////////////////////////////////////////
// Set java.library.path to include the directory where this this file
//
// libJNISpice.so (Linux, Mac?)
// libJNISpice.dll (Windows)
//
// resides e.g. on Linux, in BASH
//
// export LD_LIBRARY_PATH=/top/JNISpice/lib
// export LD_LIBRARY_PATH=/top/JNISpice/lib:"$LD_LIBRARY_PATH"
//
// or
//
// java -Djava.library.path=/top/JNISpice/lib/ altaz2nrq ...
//
//////////////////////////////////////////////////////////////////////
static
{
System.loadLibrary("JNISpice");
}
}
"""
Purpose:
Convert nadir-relative Altitude and Azimuth to
nadir-relative quaternion
Usage:
python altaz2nrq.py Alt Az MuNaught
Alt - Altitude = angular offset of [S/C +Z] from [NADIR_SUN +Z], degrees
Az - Azimuth => direction of offset of [S/C +Z] from [NADIR_SUN +Z], degrees
- Zero => Offset is toward [NADIR_SUN +X] i.e. toward Sun
- Increasing (positive) toward [NADIR_SUN +Y]
MuNaught - cosine of BennuCenter<-ORX->Sun angle
- equal to Z-component, in ORX_BENNU_SUN frame, of unit vector
pointing from ORX to Sun
- Range (-1:+1)
- Zero for terminator orbit and terminator crossings
"""
import os
import sys
import math
import spiceypy as sp
rpd = sp.rpd()
def altaz2nrq(altAzMu0):
altDeg,azDeg,mu0 = altAzMu0
vScZns = sp.radrec(1,azDeg*rpd,(90.-altDeg)*rpd)
vSunns = sp.vpack(math.sqrt(1.-(mu0*mu0)),0.,mu0)
return sp.m2q(sp.xpose(sp.twovec(vScZns,3,vSunns,1)))[[1,2,3,0]]
if "__main__" == __name__:
print(altaz2nrq(map(float,sys.argv[1:4])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment