Added another C example program: positions.c.

This example demonstrates how to calculate equatorial coordinates
and horizontal coordinates of solar system bodies.

Added explanatory comments to moonphase.c.

Added #defines for MIN_YEAR, MAX_YEAR in astronomy.h.

Removed unnecessary code from positions.html; no longer need
to calculate geocentric vector before calculating equatorial coordinates.
This commit is contained in:
Don Cross
2019-06-11 20:31:22 -04:00
parent 88a352bf90
commit 18251d16a4
9 changed files with 144 additions and 1 deletions

View File

@@ -206,7 +206,6 @@
for (let body of Astronomy.Bodies) {
if (body !== 'Earth') {
let gv = Astronomy.GeoVector(body, date);
let equ_2000 = Astronomy.Equator(body, date, observer, false, true);
let equ_ofdate = Astronomy.Equator(body, date, observer, true, true);
let hor = Astronomy.Horizon(date, observer, equ_ofdate.ra, equ_ofdate.dec, 'normal');

View File

@@ -6,6 +6,11 @@
This example shows how to determine the Moon's current phase,
and how to predict when the next few quarter phases will occur.
### [Body Positions](https://github.com/cosinekitty/astronomy/blob/master/demo/c/positions.c)
An example of how to calculate equatorial and horizontal coordinates for all of the major solar system bodies.
---
# [API Reference](../../source/c/)
Complete documentation for all the functions and types available
in the C version of Astronomy Engine.

View File

@@ -0,0 +1,53 @@
#include <stdio.h>
#include "astronomy.h"
int ParseArgs(int argc, const char *argv[], astro_observer_t *observer, astro_time_t *time)
{
if (argc == 3 || argc == 4)
{
observer->height = 0.0;
if (1 != sscanf(argv[1], "%lf", &observer->latitude) ||
observer->latitude < -90.0 ||
observer->latitude > +90.0)
{
fprintf(stderr, "ERROR: Invalid latitude '%s' on command line\n", argv[1]);
return 1;
}
if (1 != sscanf(argv[2], "%lf", &observer->longitude) ||
observer->longitude < -180.0 ||
observer->longitude > +180.0)
{
fprintf(stderr, "ERROR: Invalid longitude '%s' on command line\n", argv[2]);
return 1;
}
if (argc == 4)
{
/* Time is present on command line, so use it. */
astro_utc_t utc;
int nscanned = sscanf(argv[3], "%d-%d-%dT%d:%d:%lfZ",
&utc.year, &utc.month, &utc.day,
&utc.hour, &utc.minute, &utc.second);
if (nscanned != 6)
{
fprintf(stderr, "ERROR: Invalid date/time format in '%s'\n", argv[3]);
return 1;
}
*time = Astronomy_TimeFromUtc(utc);
}
else
{
/* Time is absent on command line, so use current time. */
*time = Astronomy_CurrentTime();
}
return 0;
}
fprintf(stderr, "USAGE: %s latitude longitude [yyyy-mm-ddThh:mm:ssZ]\n", argv[0]);
return 1;
}

View File

@@ -0,0 +1,7 @@
#ifndef __ASTRONOMY_DEMO_COMMON_H
#define __ASTRONOMY_DEMO_COMMON_H
#include "astronomy.h"
int ParseArgs(int argc, const char *argv[], astro_observer_t *observer, astro_time_t *time);
#endif /* __ASTRONOMY_DEMO_COMMON_H */

View File

@@ -3,6 +3,9 @@
Example C program for Astronomy Engine:
https://cosinekitty.github.io/astronomy/
This program calculates the Moon's phase for the current date and time.
It also finds the dates and times of the next 10 quarter phase changes.
*/
#include <stdio.h>

View File

@@ -1,7 +1,56 @@
/*
positions.c - by Don Cross - 2019-06-11
Example C program for Astronomy Engine:
https://cosinekitty.github.io/astronomy/
Given an observer's geographic latitude and longitude,
and an optional date and time, this program displays the
equatorial and horizontal coordinates of the Sun, Moon, and planets.
If the date and time is omitted from the command line, the
program uses the computer's current date and time.
*/
#include <stdio.h>
#include "astronomy.h"
#include "astro_demo_common.h"
int main(int argc, const char *argv[])
{
int error;
astro_observer_t observer;
astro_time_t time;
astro_body_t body;
astro_equatorial_t equ_2000, equ_ofdate;
astro_horizon_t hor;
error = ParseArgs(argc, argv, &observer, &time);
if (error)
return error;
printf("body RA DEC azimuth altitude\n");
for (body = MIN_BODY; body <= MAX_BODY; ++body)
{
if (body != BODY_EARTH)
{
equ_2000 = Astronomy_Equator(body, time, observer, EQUATOR_J2000, ABERRATION);
if (equ_2000.status != ASTRO_SUCCESS)
{
fprintf(stderr, "ERROR: Astronomy_Equator returned status %d trying to get J2000 coordinates.\n", equ_2000.status);
return 1;
}
equ_ofdate = Astronomy_Equator(body, time, observer, EQUATOR_OF_DATE, ABERRATION);
if (equ_ofdate.status != ASTRO_SUCCESS)
{
fprintf(stderr, "ERROR: Astronomy_Equator returned status %d trying to get coordinates of date.\n", equ_ofdate.status);
return 1;
}
hor = Astronomy_Horizon(time, observer, equ_ofdate.ra, equ_ofdate.dec, REFRACTION_NORMAL);
printf("%-7s %6.2lf %6.2lf %7.2lf %8.2lf\n", Astronomy_BodyName(body), equ_2000.ra, equ_2000.dec, hor.azimuth, hor.altitude);
}
}
return 0;
}

View File

@@ -193,6 +193,9 @@ astro_body_t;
#define MIN_BODY BODY_MERCURY /**< Minimum valid astro_body_t value; useful for iteration. */
#define MAX_BODY BODY_MOON /**< Maximum valid astro_body_t value; useful for iteration. */
#define MIN_YEAR 1700 /**< Minimum year value supported by Astronomy Engine. */
#define MAX_YEAR 2200 /**< Maximum year value supported by Astronomy Engine. */
/**
* @brief The location of an observer on (or near) the surface of the Earth.
*

View File

@@ -75,6 +75,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -83,6 +84,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -93,6 +95,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -107,6 +110,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -114,7 +118,13 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\demo\c\astro_demo_common.c" />
<ClCompile Include="..\..\..\demo\c\positions.c" />
<ClCompile Include="..\..\..\source\c\astronomy.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\demo\c\astro_demo_common.h" />
<ClInclude Include="..\..\..\source\c\astronomy.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -18,5 +18,19 @@
<ClCompile Include="..\..\..\demo\c\positions.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\c\astronomy.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\demo\c\astro_demo_common.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\demo\c\astro_demo_common.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\c\astronomy.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>