star-cem

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

test_scem_sun_position.c (2859B)


      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 
     37   utc.tm_sec = 0;
     38   utc.tm_min = 28;
     39   utc.tm_hour = 7;
     40   utc.tm_mday = 21;
     41   utc.tm_mon = 9;
     42   utc.tm_year = 115;
     43 
     44   printf("%s", asctime(&utc));
     45 
     46   pos.latitude  = 43.559962;
     47   pos.longitude = 1.468150;
     48   printf("Lat: %g°; lon: %g°\n", pos.latitude, pos.longitude);
     49 
     50   CHK(scem_sun_position_from_earth(NULL, &pos, algo, &sun) == RES_BAD_ARG);
     51   CHK(scem_sun_position_from_earth(&utc, NULL, algo, &sun) == RES_BAD_ARG);
     52   CHK(scem_sun_position_from_earth(&utc, &pos, SCEM_SUN_ALGO_NONE__, &sun)
     53       == RES_BAD_ARG);
     54   CHK(scem_sun_position_from_earth(&utc, &pos, algo, NULL) == RES_BAD_ARG);
     55 
     56   pos.latitude = nextafter(90, DBL_MAX);
     57   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     58   pos.latitude = nextafter(-90, -DBL_MAX);
     59   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     60   pos.latitude = 43.559962;
     61 
     62   pos.longitude = nextafter(180, DBL_MAX);
     63   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     64   pos.longitude = nextafter(-180, -DBL_MAX);
     65   CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_BAD_ARG);
     66   pos.longitude = 1.468150;
     67 
     68   FOR_EACH(algo, 0, SCEM_SUN_ALGO_NONE__) {
     69     CHK(scem_sun_position_from_earth(&utc, &pos, algo, &sun) == RES_OK);
     70     printf("%s - zenith: %g°; azimuth: %g°\n",
     71       scem_sun_algo_cstr(algo),
     72       MRAD2DEG(sun.zenith),
     73       MRAD2DEG(sun.azimuth));
     74   }
     75 }
     76 
     77 /*******************************************************************************
     78  * The test
     79  ******************************************************************************/
     80 int
     81 main(void)
     82 {
     83   test_api();
     84   return 0;
     85 }