star-cem

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

commit c9cc62400169dfa00258e4b10afa5615ac15f3fe
parent 5c0a994016088d8e1c89c577ad61935590aa6260
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  8 Oct 2025 09:29:44 +0200

Finalize the implementation of the scem tool

Use the scem library to calculate the position of the sun.

Add the verbose option which, when enabled, displays the UTC+00:00 date
at which the position of the sun is calculated.

Diffstat:
Mdoc/scem.1 | 7++++++-
Msrc/scem_main.c | 40++++++++++++++++++++++++++++++++++++++--
2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/doc/scem.1 b/doc/scem.1 @@ -23,7 +23,7 @@ .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" .Sh SYNOPSIS .Nm -.Op Fl hr +.Op Fl hvr .Op Fl a Ar algo .Op Fl d Ar utc_date .Ar latitude @@ -99,6 +99,11 @@ date -u +"%Y-%m-%dT%H:%M:%S" .It Fl h Output short help and exit. .\"""""""""""""""""""""""""""""""""""""" +.It Fl v +Make +.Nm +verbose. +.\"""""""""""""""""""""""""""""""""""""" .It Fl r The solar output coordinates are defined in radians rather than degrees. .El diff --git a/src/scem_main.c b/src/scem_main.c @@ -31,10 +31,11 @@ struct args { struct tm time; /* In UTC+00:00 */ enum scem_sun_algo algo; int radian; /* Sun position in radians instead of degrees */ + int verbose; int quit; }; static const struct args ARGS_DEFAULT = { - SCEM_LOCATION_NULL__, {0}, SCEM_SUN_MEEUS, 0, 0 + SCEM_LOCATION_NULL__, {0}, SCEM_SUN_MEEUS, 0, 0, 0 }; /******************************************************************************* @@ -93,6 +94,22 @@ parse_dbl } static res_T +get_current_date(struct tm* date) +{ + time_t epoch; + ASSERT(date); + + epoch = time(NULL); + if(epoch == (time_t)-1) { + fprintf(stderr, "scem: error in querying local time\n"); + return RES_BAD_ARG; + } + + *date = *gmtime(&epoch); + return RES_OK; +} + +static res_T args_init(struct args* args, int argc, char** argv) { int opt = 0; @@ -100,11 +117,14 @@ args_init(struct args* args, int argc, char** argv) *args = ARGS_DEFAULT; - while((opt = getopt(argc, argv, "a:d:hr")) != -1) { + if((res = get_current_date(&args->time)) != RES_OK) goto error; + + while((opt = getopt(argc, argv, "a:d:hvr")) != -1) { switch(opt) { case 'a': res = parse_algo(optarg, &args->algo); break; case 'd': res = parse_date(optarg, &args->time); break; case 'h': usage(stdout); args->quit = 1; goto exit; + case 'v': args->verbose = 1; break; case 'r': args->radian = 1; break; default: res = RES_BAD_ARG; break; } @@ -138,12 +158,28 @@ int main(int argc, char** argv) { struct args args = ARGS_DEFAULT; + struct scem_sun_pos pos = SCEM_SUN_POS_NULL; int err = 0; res_T res = RES_OK; if((res = args_init(&args, argc, argv)) != RES_OK) goto error; if(args.quit) goto exit; + if(args.verbose) fprintf(stderr, "%s", asctime(&args.time)); + + res = scem_sun_position_from_earth(&args.time, &args.pos, args.algo, &pos); + if(res != RES_OK) { + fprintf(stderr, "scem: error in computing the position of sun -- %s\n", + res_to_cstr(res)); + goto error; + } + + if(args.radian) { + printf("%g %g\n", pos.zenith, pos.azimuth); + } else { + printf("%g %g\n", MRAD2DEG(pos.zenith), MRAD2DEG(pos.azimuth)); + } + exit: CHK(mem_allocated_size() == 0); return err;