Add some Earth-related functions + bool in jd

This commit is contained in:
Nicolò P. 2024-07-28 12:54:41 +02:00
parent 99abab2be8
commit 77a64411e7
3 changed files with 88 additions and 5 deletions

73
src/earth.h Normal file
View File

@ -0,0 +1,73 @@
/** @file earth.h
@brief Functions related to Earth's properties
From "Astronomical Algorithms", Chapter 10 _The Earth's Globe_
*/
#include <stdlib.h>
#include <math.h>
/**
* @struct
* A representation of the Earth
* with some geometrical properties
*/
struct Earth {
double a; /*!< The Earth's equatorial radius in km */
double f; /*!< The Earth's flattening */
double b; /*!< The Earth's polar radius in km */
double e; /*!< The eccentricity of Earth's meridian */
};
/**
* @struct
* Representation of geographical
* latitude
*/
struct Latitude {
short degrees;
short arcmins;
double arcsecs;
};
/**
* @struct
* Representation of a location
* on the Earth's surface
*/
struct Location {
struct Latitude lat; /*!< The location's geographical latitude */
double h; /*!< Observer's height above sea level in m */
double r_sin_phi_p; /*!< Earth's radius times sin(geocentric latitude) */
double r_cos_phi_p; /*!< Earth's radius times cos(geocentric latitude) */
};
struct Earth set_earth_values(struct Earth * earth);
double lat_to_dec(struct Latitude lat);
struct Latitude dec_to_lat(double dec_lat);
struct Location set_location_params(struct Location * loc);
/**
* @fn
* Initializes an Earth structure
* @param *earth The Earth struct pointer
*/
struct Earth set_earth_values(struct Earth * earth) {
earth->a = 6378.14;
earth->f = 1 / 298.257;
double f = earth->f;
earth->b = earth->a * (1 - earth->f);
earth->e = sqrt(2*f - f*f);
return *earth;
}
/**
* @fn
* Convert Latitude to decimal degrees
* @param lat The Latitude struct (sessagesimal)
* @returns The converted Latitude number
*/
double lat_to_dec(struct Latitude lat) {
double dec_minutes = (double)lat.arcmins / 60.0;
double dec_seconds = lat.arcsecs / 3600;
return lat.degrees + dec_minutes + dec_seconds;
}

View File

@ -3,8 +3,8 @@
From "Astronomical Algorithms", Chapter 7 From "Astronomical Algorithms", Chapter 7
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
@ -25,8 +25,7 @@ struct Date date_from_jd(double jd);
struct Date set_month_str(struct Date *date); struct Date set_month_str(struct Date *date);
int date_diff (struct Date first, struct Date second); int date_diff (struct Date first, struct Date second);
short get_week_day(struct Date date); short get_week_day(struct Date date);
// TODO refactor to Bool... bool is_leap_year(short year);
char is_leap_year(short year);
short unsigned get_year_day(struct Date date); short unsigned get_year_day(struct Date date);
/** /**
@ -98,7 +97,13 @@ struct Date date_from_jd(double jd) {
return date; return date;
} }
/**
* @fn
* Sets the English name for the month in
* a Date struct
* @param The Date struct where the month string
* should be set
*/
struct Date set_month_str(struct Date *date) { struct Date set_month_str(struct Date *date) {
char* test; char* test;
@ -149,6 +154,8 @@ struct Date set_month_str(struct Date *date) {
} }
/** /**
* Returns the date difference in days * Returns the date difference in days
* @param first The first Date (smaller)
* @param second The second Date (larger)
*/ */
int date_diff(struct Date first, struct Date second) { int date_diff(struct Date first, struct Date second) {
double jd_first = jd(first); double jd_first = jd(first);
@ -158,6 +165,7 @@ int date_diff(struct Date first, struct Date second) {
} }
/** /**
* Returns the week day as a number * Returns the week day as a number
* @param date The Date struct to find the week day from
*/ */
short get_week_day(struct Date date) { short get_week_day(struct Date date) {
// Day should be taken at Oh // Day should be taken at Oh
@ -169,12 +177,14 @@ short get_week_day(struct Date date) {
/** /**
* Checks whether a given year is * Checks whether a given year is
* a leap year (Gregorian only!!) * a leap year (Gregorian only!!)
* @param year The year to check
*/ */
char is_leap_year(short year) { bool is_leap_year(short year) {
return (year % 4) == 0; return (year % 4) == 0;
} }
/** /**
* Returns the day number in the given year * Returns the day number in the given year
* @param date The Date struct to use
*/ */
short unsigned get_year_day(struct Date date) { short unsigned get_year_day(struct Date date) {
short k = is_leap_year(date.year) ? 1 : 2; short k = is_leap_year(date.year) ? 1 : 2;