More Earth-related functions

This commit is contained in:
Nicolò P. 2024-09-03 16:36:39 +02:00
parent 77a64411e7
commit b5d72b8ad5

View File

@ -6,10 +6,20 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
const double PI = 3.14159265365358979323;
/**
* Earth constants...
*/
const double EQ_RADIUS = 6378.14;
const double FLATTENING = 1.0 / 298.257;
const double POL_RADIUS = 6356.755;
const double RAD_RATIO = 1 - FLATTENING;
const double MERID_ECC = 0.08181922;
/** /**
* @struct * @struct
* A representation of the Earth * A representation of the Earth
* with some geometrical properties * with some geometrical properties
* @todo Useless?
*/ */
struct Earth { struct Earth {
double a; /*!< The Earth's equatorial radius in km */ double a; /*!< The Earth's equatorial radius in km */
@ -17,16 +27,15 @@ struct Earth {
double b; /*!< The Earth's polar radius in km */ double b; /*!< The Earth's polar radius in km */
double e; /*!< The eccentricity of Earth's meridian */ double e; /*!< The eccentricity of Earth's meridian */
}; };
/** /**
* @struct * @struct
* Representation of geographical * Representation of geographical
* latitude * latitude
*/ */
struct Latitude { struct Latitude {
short degrees; short degrees; /*!< Latitude degrees as an integer */
short arcmins; short arcmins; /*!< Latitude arc minutes as an integer */
double arcsecs; double arcsecs; /*!< Latitude arc seconds as decimal */
}; };
/** /**
* @struct * @struct
@ -36,27 +45,26 @@ struct Latitude {
struct Location { struct Location {
struct Latitude lat; /*!< The location's geographical latitude */ struct Latitude lat; /*!< The location's geographical latitude */
double h; /*!< Observer's height above sea level in m */ double h; /*!< Observer's height above sea level in m */
double r_sin_phi_p; /*!< Earth's radius times sin(geocentric latitude) */ double r_sin_phi_p; /*!< Earth's radius times sin(geocentric_latitude) */
double r_cos_phi_p; /*!< Earth's radius times cos(geocentric latitude) */ double r_cos_phi_p; /*!< Earth's radius times cos(geocentric_latitude) */
}; };
struct Earth set_earth_values(struct Earth * earth); void set_earth_values(struct Earth * earth);
double lat_to_dec(struct Latitude lat); double lat_to_dec(struct Latitude lat);
double r_sin_phi(double h, double decimal_lat);
struct Latitude dec_to_lat(double dec_lat); struct Latitude dec_to_lat(double dec_lat);
struct Location set_location_params(struct Location * loc); struct Location set_location_params(struct Location * loc);
/** /**
* @fn * @fn
* Initializes an Earth structure * Initializes an Earth structure
* @param *earth The Earth struct pointer * @todo Even more useless??
*/ */
struct Earth set_earth_values(struct Earth * earth) { void set_earth_values(struct Earth * earth) {
earth->a = 6378.14; earth->a = EQ_RADIUS;
earth->f = 1 / 298.257; earth->f = FLATTENING;
double f = earth->f; earth->b = POL_RADIUS;
earth->b = earth->a * (1 - earth->f); earth->e = MERID_ECC;
earth->e = sqrt(2*f - f*f);
return *earth;
} }
/** /**
* @fn * @fn
@ -70,4 +78,53 @@ double lat_to_dec(struct Latitude lat) {
return lat.degrees + dec_minutes + dec_seconds; return lat.degrees + dec_minutes + dec_seconds;
} }
/**
* @fn
* Calculates $\rho \sin{\phi}$ for
* a location
* @param h The location's height in meters
* @param decimal_lat The location's decimal latitude in deg
*/
double rho_sin_phi(double h, double decimal_lat) {
// Convert lat from deg to radians
double lat_rad = decimal_lat * (PI / 180.);
double tan_u = RAD_RATIO * tan(lat_rad);
double u = atan(tan_u);
double rho_sin_phi = RAD_RATIO * sin(u) + (h / 6378140.) * sin(lat_rad);
return rho_sin_phi;
}
/**
* @fn
* Calculates $\rho \sin{\phi}$ for
* a location
* @param h The location's height in meters
* @param decimal_lat The location's decimal latitude
*/
double rho_cos_phi(double h, double decimal_lat) {
// Convert lat from deg to radians
double lat_rad = decimal_lat * (PI / 180.);
double tan_u = RAD_RATIO * tan(lat_rad);
double u = atan(tan_u);
double rho_cos_phi = cos(u) + (h / 6378140) * cos(lat_rad);
return rho_cos_phi;
}
/**
* @fn
* Calculates and sets geographical parameters
* related to a location
* @param loc The Location to set
* @param lat The location's Latitude
* @param h The location's height in meters
*/
struct Location set_location_params(struct Location * loc) {
double decimal_lat = lat_to_dec(loc->lat);
loc->r_sin_phi_p = rho_sin_phi(loc->h, decimal_lat);
loc->r_cos_phi_p = rho_cos_phi(loc->h, decimal_lat);
return *loc;
}