star-cem

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

scem.h (2696B)


      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 #ifndef SCEM_H
     17 #define SCEM_H
     18 
     19 #include <rsys/rsys.h>
     20 
     21 #if defined(SCEM_SHARED_BUILD)
     22   #define SCEM_API extern EXPORT_SYM
     23 #else
     24   #define SCEM_API extern IMPORT_SYM
     25 #endif
     26 
     27 /* Forward declaration */
     28 struct tm;
     29 
     30 enum scem_sun_algo {
     31   /* Algorithm taken from Jean Meeus' book "Astronomical Algorithm" */
     32   SCEM_SUN_MEEUS,
     33   /* Algorithm from the "Plataforma Solar de Almería" */
     34   SCEM_SUN_PSA,
     35 
     36   SCEM_SUN_ALGO_NONE__
     37 };
     38 
     39 /* Sun position */
     40 struct scem_sun_pos {
     41   /* In [-PI/2, PI/2]. Positive toward the sky */
     42   double zenith; /* [rad] */
     43 
     44   /* In [0, 2PI] with 0 aligned with north, PI/2 east, PI south, and 3PI/2 west */
     45   double azimuth; /* [rad] */
     46 };
     47 #define SCEM_SUN_POS_NULL__ {0, 0}
     48 static const struct scem_sun_pos SCEM_SUN_POS_NULL = SCEM_SUN_POS_NULL__;
     49 
     50 /* Position defined in geographic coordinate system */
     51 struct scem_location {
     52   /* In [-180,180] decimal degree relative to Greenwitch.
     53    * Positive toward the east */
     54   double longitude;
     55   /* In [-90, 90] decimal degree relative to the equator.
     56    * Positive toward the north */
     57   double latitude;
     58 };
     59 #define SCEM_LOCATION_NULL__ {0,0}
     60 static const struct scem_location SCEM_LOCATION_NULL = SCEM_LOCATION_NULL__;
     61 
     62 static INLINE const char*
     63 scem_sun_algo_cstr(const enum scem_sun_algo algo)
     64 {
     65   const char* cstr = "None";
     66   switch(algo) {
     67     case SCEM_SUN_MEEUS: cstr = "Meeus"; break;
     68     case SCEM_SUN_PSA: cstr = "PSA"; break;
     69     default: FATAL("Unreachable code\n"); break;
     70   }
     71   return cstr;
     72 }
     73 
     74 BEGIN_DECLS
     75 
     76 /* Compute the position of the Sun in local coordinates (zenith and azimuth) for
     77  * a given observer at the surface of the Earth (can be extended to other
     78  * planets if required) i.e. the observer has to be located at the surface for
     79  * the required date */
     80 SCEM_API res_T
     81 scem_sun_position_from_earth
     82   (const struct tm* time, /* In UTC */
     83    const struct scem_location* position, /* Local position */
     84    const enum scem_sun_algo algo,
     85    struct scem_sun_pos* sun);
     86 
     87 END_DECLS
     88 
     89 #endif /* SCEM_H */