star-cem

Compute the position of the sun
git clone git://git.meso-star.fr/star-cem.git
Log | Files | Refs | README | LICENSE

commit b761e43b12f4635bd7638e7b0fd06bca62f388ff
parent 73ba2001efa3bb7577037015ec14963ab7ab7176
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  8 Oct 2025 14:47:22 +0200

Check the coordinates of the entry position.

Ensure that they are valid in relation to their range.

Diffstat:
MMakefile | 2+-
Msrc/scem.c | 6++++++
Msrc/test_scem_sun_position.c | 42++++++++++++++++++++++++++++--------------
3 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile @@ -154,7 +154,7 @@ INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys scem-local) LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys scem-local) CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST) -LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -lm tests: library $(TEST_DEP) $(TEST_TGT) @$(MAKE) -fMakefile \ diff --git a/src/scem.c b/src/scem.c @@ -31,6 +31,12 @@ scem_sun_position_from_earth if(!time || !pos || !sun || (unsigned)algo >= SCEM_SUN_ALGO_NONE__) return RES_BAD_ARG; + if(pos->latitude < -90 + || pos->latitude > +90 + || pos->longitude < -180 + || pos->longitude > +180) + return RES_BAD_ARG; + switch(algo) { case SCEM_SUN_MEEUS: res = sun_position_from_earth_meeus(time, pos, sun); diff --git a/src/test_scem_sun_position.c b/src/test_scem_sun_position.c @@ -13,11 +13,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _POSIX_C_SOURCE 200112L /* nextafter support */ + #include "scem.h" #include <rsys/math.h> #include <rsys/rsys.h> +#include <math.h> #include <time.h> /******************************************************************************* @@ -26,26 +29,22 @@ static void test_api(void) { - time_t epoch; enum scem_sun_algo algo = SCEM_SUN_MEEUS; struct tm utc = {0}; struct scem_location pos = SCEM_LOCATION_NULL; struct scem_sun_pos sun = SCEM_SUN_POS_NULL; - epoch = time(NULL); - CHK(epoch != ((time_t)-1)); - utc.tm_sec = 0; utc.tm_min = 28; - utc.tm_hour = 17; - utc.tm_mday = 6; + utc.tm_hour = 7; + utc.tm_mday = 21; utc.tm_mon = 9; - utc.tm_year = 125; + utc.tm_year = 115; printf("%s", asctime(&utc)); - pos.latitude = 43.60460; - pos.longitude = 1.44422; + pos.latitude = 43.559962; + pos.longitude = 1.468150; printf("Lat: %g°; lon: %g°\n", pos.latitude, pos.longitude); CHK(scem_sun_position_from_earth(NULL, &pos, algo, &sun) == RES_BAD_ARG); @@ -53,11 +52,26 @@ test_api(void) CHK(scem_sun_position_from_earth(&utc, &pos, SCEM_SUN_ALGO_NONE__, &sun) == RES_BAD_ARG); CHK(scem_sun_position_from_earth(&utc, &pos, algo, NULL) == RES_BAD_ARG); - CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_OK); - printf("%s - azimuth: %g°; elevation: %g°\n", - scem_sun_algo_cstr(algo), - MRAD2DEG(sun.azimuth), - MRAD2DEG(sun.zenith)); + + pos.latitude = nextafter(90, DBL_MAX); + CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG); + pos.latitude = nextafter(-90, -DBL_MAX); + CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG); + pos.latitude = 43.559962; + + pos.longitude = nextafter(180, DBL_MAX); + CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG); + pos.longitude = nextafter(-180, -DBL_MAX); + CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG); + pos.longitude = 1.468150; + + FOR_EACH(algo, 0, SCEM_SUN_ALGO_NONE__) { + CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_OK); + printf("%s - zenith: %g°; azimuth: %g°\n", + scem_sun_algo_cstr(algo), + MRAD2DEG(sun.zenith), + MRAD2DEG(sun.azimuth)); + } } /*******************************************************************************