star-cem

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

test_scem_sun_position.c (3190B)


      1 /* Copyright (C) 2025 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #define _POSIX_C_SOURCE 200112L /* nextafter support */
     17 
     18 #include "scem.h"
     19 
     20 #include <rsys/math.h>
     21 #include <rsys/rsys.h>
     22 
     23 #include <math.h>
     24 #include <time.h>
     25 
     26 /*******************************************************************************
     27  * Helper functions
     28  ******************************************************************************/
     29 static void
     30 test_api(void)
     31 {
     32   enum scem_sun_algo algo = SCEM_SUN_MEEUS;
     33   struct tm utc = {0};
     34   struct scem_location pos = SCEM_LOCATION_NULL;
     35   struct scem_sun_pos sun = SCEM_SUN_POS_NULL;
     36   double vec[3];
     37 
     38   utc.tm_sec = 0;
     39   utc.tm_min = 28;
     40   utc.tm_hour = 7;
     41   utc.tm_mday = 21;
     42   utc.tm_mon = 9;
     43   utc.tm_year = 115;
     44 
     45   printf("%s", asctime(&utc));
     46 
     47   pos.latitude  = 43.559962;
     48   pos.longitude = 1.468150;
     49   printf("Lat: %g°; lon: %g°\n", pos.latitude, pos.longitude);
     50 
     51   CHK(scem_sun_position_to_sun_vector(NULL, NULL) == RES_BAD_ARG);
     52   CHK(scem_sun_position_to_sun_vector(&sun, NULL) == RES_BAD_ARG);
     53   CHK(scem_sun_position_to_sun_vector(NULL, vec) == RES_BAD_ARG);
     54 
     55   CHK(scem_sun_position_from_earth(NULL, &pos, algo, &sun) == RES_BAD_ARG);
     56   CHK(scem_sun_position_from_earth(&utc, NULL, algo, &sun) == RES_BAD_ARG);
     57   CHK(scem_sun_position_from_earth(&utc, &pos, SCEM_SUN_ALGO_NONE__, &sun)
     58       == RES_BAD_ARG);
     59   CHK(scem_sun_position_from_earth(&utc, &pos, algo, NULL) == RES_BAD_ARG);
     60 
     61   pos.latitude = nextafter(90, DBL_MAX);
     62   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     63   pos.latitude = nextafter(-90, -DBL_MAX);
     64   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     65   pos.latitude = 43.559962;
     66 
     67   pos.longitude = nextafter(180, DBL_MAX);
     68   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     69   pos.longitude = nextafter(-180, -DBL_MAX);
     70   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     71   pos.longitude = 1.468150;
     72 
     73   FOR_EACH(algo, 0, SCEM_SUN_ALGO_NONE__) {
     74     CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_OK);
     75     printf("%s - zenith: %g°; azimuth: %g°\n",
     76       scem_sun_algo_cstr(algo),
     77       MRAD2DEG(sun.elevation),
     78       MRAD2DEG(sun.azimuth));
     79     CHK(scem_sun_position_to_sun_vector(&sun, vec) == RES_OK);
     80     printf("Vector: %g %g %g\n", SPLIT3(vec));
     81   }
     82 }
     83 
     84 /*******************************************************************************
     85  * The test
     86  ******************************************************************************/
     87 int
     88 main(void)
     89 {
     90   test_api();
     91   return 0;
     92 }