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

Add novas_planet_for_name() #87

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
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
Add novas_planet_for_name()
  • Loading branch information
attipaci committed Nov 10, 2024
commit f5170c47a3b3d352c0175985a263f1aacee32097
1 change: 1 addition & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CC ?= gcc
CPPFLAGS += -I$(INC)

# Base compiler options (if not defined externally...)
# -std=c99 may not be supported by some very old compilers...
CFLAGS ?= -Os -Wall -std=c99

# Extra warnings (not supported on all compilers)
Expand Down
4 changes: 2 additions & 2 deletions include/novas.h
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,6 @@ int make_redshifted_object(const char *name, double ra, double dec, double z, ob

double novas_z2v(double z);


// in util.c
double novas_v2z(double vel);

double grav_redshift(double M_kg, double r_m);
Expand All @@ -1299,6 +1297,8 @@ double novas_z_add(double z1, double z2);

double novas_z_inv(double z);

enum novas_planet novas_planet_for_name(const char *name);


// <================= END of SuperNOVAS API =====================>

Expand Down
13 changes: 11 additions & 2 deletions src/solsys-calceph.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ static short planet_calceph(double jd_tdb, enum novas_planet body, enum novas_or
* when CALCEPH and the ephemeris data may not be. When necessary, the ephemeris access will be
* mutexed to ensure sequential access under the hood.
*
* @param name The name of the solar-system body. It is important only if the id is
* @param name The name of the solar-system body. It is important only if the 'id' is
* -1.
* @param id The NAIF ID number of the solar-system body for which the position in
* desired, or -1 if the name should be used instead to identify the
* desired, or -1 if the 'name' should be used instead to identify the
* object.
* @param jd_tdb_high [day] The high-order part of Barycentric Dynamical Time (TDB) based
* Julian date for which to find the position and velocity. Typically
Expand Down Expand Up @@ -301,9 +301,18 @@ static int novas_calceph(const char *name, long id, double jd_tdb_high, double j
int i, success, center;

if(id == -1) {
// Lookup by name...

if(!name)
return novas_error(-1, EINVAL, fn, "id=-1 and name is NULL");

if(!name[0])
return novas_error(-1, EINVAL, fn, "id=-1 and name is empty");

// Use name to get NAIF ID.
if(!calceph_getidbyname(bodies, name, compute_flags, &i))
return novas_error(1, EINVAL, fn, "CALCEPH could not find a NAIF ID for '%s'", name);

id = i;
}

Expand Down
52 changes: 52 additions & 0 deletions src/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
* can live in a separate, more manageably sized, module.
*/

// We'll use gcc major version as a proxy for the glibc library to decide which feature macro to use.
// gcc 5.1 was released 2015-04-22...
#if __GNUC__ >= 5
# define _DEFAULT_SOURCE ///< strcasdecmp() feature macro starting glibc 2.20 (2014-09-08)
#else
# define _BSD_SOURCE ///< strcasecmp() feature macro for glibc <= 2.19
#endif

#include <math.h>
#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -1277,3 +1285,47 @@ double novas_z_inv(double z) {
return 1.0 / (1.0 + z) - 1.0;
}

/**
* Returns the NOVAS planet ID for a given name (case insensitive), or -1 if no match is found.
*
* @param name The planet name, or that for the "Sun", "Moon" or "SSB" (case insensitive).
* The spelled out "Solar System Barycenter" is also recognized with either spaces,
* hyphens ('-') or underscores ('_') separating the case insensitive words.
* @return The NOVAS major planet ID, or -1 (errno set to EINVAL) if the input name is
* NULL or if there is no match for the name provided.
*
* @author Attila Kovacs
* @since 1.2
*
* @sa make_planet()
*/
enum novas_planet novas_planet_for_name(const char *name) {
static const char *fn = "novas_planet_for_name()";
static const char *names[] = NOVAS_PLANET_NAMES_INIT;

char *tok;
int i;

if(!name)
return novas_error(-1, EINVAL, fn, "Input name is NULL");

if(!name[0])
return novas_error(-1, EINVAL, fn, "Input name is empty");

for(i = 0; i < NOVAS_PLANETS; i++)
if(strcasecmp(name, (const char *) names[i]) == 0)
return i;

// Check for Solar System Barycenter (and variants)
tok = strtok(strdup(name), " \t-_");
if(strcasecmp("solar", tok) == 0) {
tok = strtok(NULL, " \t-_");
if(tok && strcasecmp("system", tok) == 0) {
tok = strtok(NULL, " \t-_");
if(tok && strcasecmp("barycenter", tok) == 0)
return NOVAS_SSB;
}
}

return novas_error(-1, EINVAL, fn, "No match for name: '%s'", name);
}
9 changes: 6 additions & 3 deletions test/src/test-calceph.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,17 @@ static int test_errors() {

calceph_seterrorhandler(3, dummy_error_handler);

if(check("errors:body:name:NULL", -1, eph(NULL, -1, jd2[0], jd2[1], NOVAS_BARYCENTER, pos, vel))) return 1;

if(check("errors:body:name:empty", -1, eph("", -1, jd2[0], jd2[1], NOVAS_BARYCENTER, pos, vel))) return 1;

if(check("errors:body:name:nomatch", 1, eph("blah", -1, jd2[0], jd2[1], NOVAS_BARYCENTER, pos, vel))) return 1;

jd2[0] = -999999.0;
if(check("errors:planet:time", 3, pl(jd2, NOVAS_MARS, NOVAS_BARYCENTER, pos, vel))) return 1;

if(check("errors:body:time", 3, eph("phobos", 401, jd2[0], jd2[1], NOVAS_BARYCENTER, pos, vel))) return 1;

if(check("errors:body:name", 1, eph("blah", -1, jd2[0], jd2[1], NOVAS_BARYCENTER, pos, vel))) return 1;


return 0;
}

Expand Down
16 changes: 16 additions & 0 deletions test/src/test-errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,20 @@ static int test_naif_to_novas_planet() {
return n;
}

static int test_planet_for_name() {
int n = 0;

if(check("planet_for_name:NULL", -1, novas_planet_for_name(NULL))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name(""))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name("blah"))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name("solar"))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name("Solar flare"))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name("Solar system"))) n++;
if(check("planet_for_name:blah", -1, novas_planet_for_name("Solar system size"))) n++;

return n;
}

int main() {
int n = 0;

Expand Down Expand Up @@ -1606,6 +1620,8 @@ int main() {
if(test_novas_to_dexxx_planet()) n++;
if(test_naif_to_novas_planet()) n++;

if(test_planet_for_name()) n++;

if(n) fprintf(stderr, " -- FAILED %d tests\n", n);
else fprintf(stderr, " -- OK\n");

Expand Down
114 changes: 66 additions & 48 deletions test/src/test-super.c
Original file line number Diff line number Diff line change
Expand Up @@ -2132,68 +2132,84 @@ static int test_z_inv() {
}

static int test_novas_to_naif_planet() {
int n = 0;

if(!is_ok("novas_to_naif_planet:ssb", novas_to_naif_planet(NOVAS_SSB) != NAIF_SSB)) return 1;
if(!is_ok("novas_to_naif_planet:sun", novas_to_naif_planet(NOVAS_SUN) != NAIF_SUN)) return 1;
if(!is_ok("novas_to_naif_planet:moon", novas_to_naif_planet(NOVAS_MOON) != NAIF_MOON)) return 1;
if(!is_ok("novas_to_naif_planet:earth", novas_to_naif_planet(NOVAS_EARTH) != NAIF_EARTH)) return 1;
if(!is_ok("novas_to_naif_planet:mercury", novas_to_naif_planet(NOVAS_MERCURY) != 199)) return 1;
if(!is_ok("novas_to_naif_planet:venus", novas_to_naif_planet(NOVAS_VENUS) != 299)) return 1;
if(!is_ok("novas_to_naif_planet:mars", novas_to_naif_planet(NOVAS_MARS) != 499)) return 1;
if(!is_ok("novas_to_naif_planet:jupiter", novas_to_naif_planet(NOVAS_JUPITER) != 599)) return 1;
if(!is_ok("novas_to_naif_planet:saturn", novas_to_naif_planet(NOVAS_SATURN) != 699)) return 1;
if(!is_ok("novas_to_naif_planet:uranus", novas_to_naif_planet(NOVAS_URANUS) != 799)) return 1;
if(!is_ok("novas_to_naif_planet:neptune", novas_to_naif_planet(NOVAS_NEPTUNE) != 899)) return 1;
if(!is_ok("novas_to_naif_planet:pluto", novas_to_naif_planet(NOVAS_PLUTO) != 999)) return 1;
if(!is_ok("novas_to_naif_planet:ssb", novas_to_naif_planet(NOVAS_SSB) != NAIF_SSB)) n++;
if(!is_ok("novas_to_naif_planet:sun", novas_to_naif_planet(NOVAS_SUN) != NAIF_SUN)) n++;
if(!is_ok("novas_to_naif_planet:moon", novas_to_naif_planet(NOVAS_MOON) != NAIF_MOON)) n++;
if(!is_ok("novas_to_naif_planet:earth", novas_to_naif_planet(NOVAS_EARTH) != NAIF_EARTH)) n++;
if(!is_ok("novas_to_naif_planet:mercury", novas_to_naif_planet(NOVAS_MERCURY) != 199)) n++;
if(!is_ok("novas_to_naif_planet:venus", novas_to_naif_planet(NOVAS_VENUS) != 299)) n++;
if(!is_ok("novas_to_naif_planet:mars", novas_to_naif_planet(NOVAS_MARS) != 499)) n++;
if(!is_ok("novas_to_naif_planet:jupiter", novas_to_naif_planet(NOVAS_JUPITER) != 599)) n++;
if(!is_ok("novas_to_naif_planet:saturn", novas_to_naif_planet(NOVAS_SATURN) != 699)) n++;
if(!is_ok("novas_to_naif_planet:uranus", novas_to_naif_planet(NOVAS_URANUS) != 799)) n++;
if(!is_ok("novas_to_naif_planet:neptune", novas_to_naif_planet(NOVAS_NEPTUNE) != 899)) n++;
if(!is_ok("novas_to_naif_planet:pluto", novas_to_naif_planet(NOVAS_PLUTO) != 999)) n++;

return 0;
return n;
}

static int test_novas_to_dexxx_planet() {
int n = 0;

if(!is_ok("novas_to_dexxx_planet:ssb", novas_to_dexxx_planet(NOVAS_SSB) != NAIF_SSB)) return 1;
if(!is_ok("novas_to_dexxx_planet:sun", novas_to_dexxx_planet(NOVAS_SUN) != NAIF_SUN)) return 1;
if(!is_ok("novas_to_dexxx_planet:moon", novas_to_dexxx_planet(NOVAS_MOON) != NAIF_MOON)) return 1;
if(!is_ok("novas_to_dexxx_planet:earth", novas_to_dexxx_planet(NOVAS_EARTH) != NAIF_EARTH)) return 1;
if(!is_ok("novas_to_dexxx_planet:mercury", novas_to_dexxx_planet(NOVAS_MERCURY) != 1)) return 1;
if(!is_ok("novas_to_dexxx_planet:venus", novas_to_dexxx_planet(NOVAS_VENUS) != 2)) return 1;
if(!is_ok("novas_to_dexxx_planet:mars", novas_to_dexxx_planet(NOVAS_MARS) != 4)) return 1;
if(!is_ok("novas_to_dexxx_planet:jupiter", novas_to_dexxx_planet(NOVAS_JUPITER) != 5)) return 1;
if(!is_ok("novas_to_dexxx_planet:saturn", novas_to_dexxx_planet(NOVAS_SATURN) != 6)) return 1;
if(!is_ok("novas_to_dexxx_planet:uranus", novas_to_dexxx_planet(NOVAS_URANUS) != 7)) return 1;
if(!is_ok("novas_to_dexxx_planet:neptune", novas_to_dexxx_planet(NOVAS_NEPTUNE) != 8)) return 1;
if(!is_ok("novas_to_dexxx_planet:pluto", novas_to_dexxx_planet(NOVAS_PLUTO) != 9)) return 1;
if(!is_ok("novas_to_dexxx_planet:ssb", novas_to_dexxx_planet(NOVAS_SSB) != NAIF_SSB)) n++;
if(!is_ok("novas_to_dexxx_planet:sun", novas_to_dexxx_planet(NOVAS_SUN) != NAIF_SUN)) n++;
if(!is_ok("novas_to_dexxx_planet:moon", novas_to_dexxx_planet(NOVAS_MOON) != NAIF_MOON)) n++;
if(!is_ok("novas_to_dexxx_planet:earth", novas_to_dexxx_planet(NOVAS_EARTH) != NAIF_EARTH)) n++;
if(!is_ok("novas_to_dexxx_planet:mercury", novas_to_dexxx_planet(NOVAS_MERCURY) != 1)) n++;
if(!is_ok("novas_to_dexxx_planet:venus", novas_to_dexxx_planet(NOVAS_VENUS) != 2)) n++;
if(!is_ok("novas_to_dexxx_planet:mars", novas_to_dexxx_planet(NOVAS_MARS) != 4)) n++;
if(!is_ok("novas_to_dexxx_planet:jupiter", novas_to_dexxx_planet(NOVAS_JUPITER) != 5)) n++;
if(!is_ok("novas_to_dexxx_planet:saturn", novas_to_dexxx_planet(NOVAS_SATURN) != 6)) n++;
if(!is_ok("novas_to_dexxx_planet:uranus", novas_to_dexxx_planet(NOVAS_URANUS) != 7)) n++;
if(!is_ok("novas_to_dexxx_planet:neptune", novas_to_dexxx_planet(NOVAS_NEPTUNE) != 8)) n++;
if(!is_ok("novas_to_dexxx_planet:pluto", novas_to_dexxx_planet(NOVAS_PLUTO) != 9)) n++;

return 0;
return n;
}

static int test_naif_to_novas_planet() {
int n = 0;

if(!is_ok("naif_to_novas_planet:ssb", naif_to_novas_planet(NAIF_SSB) != NOVAS_SSB)) return 1;
if(!is_ok("naif_to_novas_planet:sun", naif_to_novas_planet(NAIF_SUN) != NOVAS_SUN)) return 1;
if(!is_ok("naif_to_novas_planet:moon", naif_to_novas_planet(NAIF_MOON) != NOVAS_MOON)) return 1;
if(!is_ok("naif_to_novas_planet:earth", naif_to_novas_planet(NAIF_EARTH) != NOVAS_EARTH)) return 1;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(199) != NOVAS_MERCURY)) return 1;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(299) != NOVAS_VENUS)) return 1;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(499) != NOVAS_MARS)) return 1;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(599) != NOVAS_JUPITER)) return 1;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(699) != NOVAS_SATURN)) return 1;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(799) != NOVAS_URANUS)) return 1;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(899) != NOVAS_NEPTUNE)) return 1;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(999) != NOVAS_PLUTO)) return 1;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(1) != NOVAS_MERCURY)) return 1;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(2) != NOVAS_VENUS)) return 1;
if(!is_ok("naif_to_novas_planet:emb", naif_to_novas_planet(3) != -1)) return 1;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(4) != NOVAS_MARS)) return 1;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(5) != NOVAS_JUPITER)) return 1;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(6) != NOVAS_SATURN)) return 1;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(7) != NOVAS_URANUS)) return 1;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(8) != NOVAS_NEPTUNE)) return 1;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(9) != NOVAS_PLUTO)) return 1;
if(!is_ok("naif_to_novas_planet:ssb", naif_to_novas_planet(NAIF_SSB) != NOVAS_SSB)) n++;
if(!is_ok("naif_to_novas_planet:sun", naif_to_novas_planet(NAIF_SUN) != NOVAS_SUN)) n++;
if(!is_ok("naif_to_novas_planet:moon", naif_to_novas_planet(NAIF_MOON) != NOVAS_MOON)) n++;
if(!is_ok("naif_to_novas_planet:earth", naif_to_novas_planet(NAIF_EARTH) != NOVAS_EARTH)) n++;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(199) != NOVAS_MERCURY)) n++;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(299) != NOVAS_VENUS)) n++;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(499) != NOVAS_MARS)) n++;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(599) != NOVAS_JUPITER)) n++;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(699) != NOVAS_SATURN)) n++;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(799) != NOVAS_URANUS)) n++;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(899) != NOVAS_NEPTUNE)) n++;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(999) != NOVAS_PLUTO)) n++;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(1) != NOVAS_MERCURY)) n++;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(2) != NOVAS_VENUS)) n++;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(4) != NOVAS_MARS)) n++;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(5) != NOVAS_JUPITER)) n++;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(6) != NOVAS_SATURN)) n++;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(7) != NOVAS_URANUS)) n++;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(8) != NOVAS_NEPTUNE)) n++;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(9) != NOVAS_PLUTO)) n++;

return 0;
return n;
}

static int test_planet_for_name() {
int n = 0;

if(!is_ok("planet_for_name:mercury", novas_planet_for_name("mercury") != NOVAS_MERCURY)) n++;
if(!is_ok("planet_for_name:pluto", novas_planet_for_name("PLUTO") != NOVAS_PLUTO)) n++;
if(!is_ok("planet_for_name:sun", novas_planet_for_name("Sun") != NOVAS_SUN)) n++;
if(!is_ok("planet_for_name:moon", novas_planet_for_name("MooN") != NOVAS_MOON)) n++;
if(!is_ok("planet_for_name:ssb", novas_planet_for_name("SSB") != NOVAS_SSB)) n++;
if(!is_ok("planet_for_name:ssb1", novas_planet_for_name("Solar-system barycenter") != NOVAS_SSB)) n++;

return n;
}


int main(int argc, char *argv[]) {
int n = 0;

Expand Down Expand Up @@ -2257,6 +2273,8 @@ int main(int argc, char *argv[]) {
if(test_novas_to_dexxx_planet()) n++;
if(test_naif_to_novas_planet()) n++;

if(test_planet_for_name()) n++;

n += test_dates();

return n;
Expand Down