Practical Astronomy Algorithms in .NET/C#
Loading...
Searching...
No Matches
PASun.cs
Go to the documentation of this file.
1using System;
2using PALib.Helpers;
3
4namespace PALib;
5
9public class PASun
10{
30 public (double sunRAHour, double sunRAMin, double sunRASec, double sunDecDeg, double sunDecMin, double sunDecSec) ApproximatePositionOfSun(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
31 {
32 int daylightSaving = (isDaylightSaving == true) ? 1 : 0;
33
34 double greenwichDateDay = PAMacros.LocalCivilTimeGreenwichDay(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
35 int greenwichDateMonth = PAMacros.LocalCivilTimeGreenwichMonth(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
36 int greenwichDateYear = PAMacros.LocalCivilTimeGreenwichYear(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
37 double utHours = PAMacros.LocalCivilTimeToUniversalTime(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
38 double utDays = utHours / 24;
39 double jdDays = PAMacros.CivilDateToJulianDate(greenwichDateDay, greenwichDateMonth, greenwichDateYear) + utDays;
40 double dDays = jdDays - PAMacros.CivilDateToJulianDate(0, 1, 2010);
41 double nDeg = 360 * dDays / 365.242191;
42 double mDeg1 = nDeg + PAMacros.SunELong(0, 1, 2010) - PAMacros.SunPeri(0, 1, 2010);
43 double mDeg2 = mDeg1 - 360 * (mDeg1 / 360).Floor();
44 double eCDeg = 360 * PAMacros.SunEcc(0, 1, 2010) * mDeg2.ToRadians().Sine() / Math.PI;
45 double lSDeg1 = nDeg + eCDeg + PAMacros.SunELong(0, 1, 2010);
46 double lSDeg2 = lSDeg1 - 360 * (lSDeg1 / 360).Floor();
47 double raDeg = PAMacros.EcRA(lSDeg2, 0, 0, 0, 0, 0, greenwichDateDay, greenwichDateMonth, greenwichDateYear);
48 double raHours = PAMacros.DecimalDegreesToDegreeHours(raDeg);
49 double decDeg = PAMacros.EcDec(lSDeg2, 0, 0, 0, 0, 0, greenwichDateDay, greenwichDateMonth, greenwichDateYear);
50
51 int sunRAHour = PAMacros.DecimalHoursHour(raHours);
53 double sunRASec = PAMacros.DecimalHoursSecond(raHours);
56 double sunDecSec = PAMacros.DecimalDegreesSeconds(decDeg);
57
58 return (sunRAHour, sunRAMin, sunRASec, sunDecDeg, sunDecMin, sunDecSec);
59 }
60
64 public (double sunRAHour, double sunRAMin, double sunRASec, double sunDecDeg, double sunDecMin, double sunDecSec) PrecisePositionOfSun(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
65 {
66 int daylightSaving = (isDaylightSaving == true) ? 1 : 0;
67
68 double gDay = PAMacros.LocalCivilTimeGreenwichDay(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
69 int gMonth = PAMacros.LocalCivilTimeGreenwichMonth(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
70 int gYear = PAMacros.LocalCivilTimeGreenwichYear(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
71 double sunEclipticLongitudeDeg = PAMacros.SunLong(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
72 double raDeg = PAMacros.EcRA(sunEclipticLongitudeDeg, 0, 0, 0, 0, 0, gDay, gMonth, gYear);
73 double raHours = PAMacros.DecimalDegreesToDegreeHours(raDeg);
74 double decDeg = PAMacros.EcDec(sunEclipticLongitudeDeg, 0, 0, 0, 0, 0, gDay, gMonth, gYear);
75
76 int sunRAHour = PAMacros.DecimalHoursHour(raHours);
78 double sunRASec = PAMacros.DecimalHoursSecond(raHours);
81 double sunDecSec = PAMacros.DecimalDegreesSeconds(decDeg);
82
83 return (sunRAHour, sunRAMin, sunRASec, sunDecDeg, sunDecMin, sunDecSec);
84 }
85
95 public (double sunDistKm, double sunAngSizeDeg, double sunAngSizeMin, double sunAngSizeSec) SunDistanceAndAngularSize(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
96 {
97 int daylightSaving = isDaylightSaving ? 1 : 0;
98
99 double gDay = PAMacros.LocalCivilTimeGreenwichDay(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
100 int gMonth = PAMacros.LocalCivilTimeGreenwichMonth(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
101 int gYear = PAMacros.LocalCivilTimeGreenwichYear(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
102 double trueAnomalyDeg = PAMacros.SunTrueAnomaly(lctHours, lctMinutes, lctSeconds, daylightSaving, zoneCorrection, localDay, localMonth, localYear);
103 double trueAnomalyRad = trueAnomalyDeg.ToRadians();
104 double eccentricity = PAMacros.SunEcc(gDay, gMonth, gYear);
105 double f = (1 + eccentricity * trueAnomalyRad.Cosine()) / (1 - eccentricity * eccentricity);
106 double rKm = 149598500 / f;
107 double thetaDeg = f * 0.533128;
108
109 double sunDistKm = Math.Round(rKm, 0);
112 double sunAngSizeSec = PAMacros.DecimalDegreesSeconds(thetaDeg);
113
114 return (sunDistKm, sunAngSizeDeg, sunAngSizeMin, sunAngSizeSec);
115 }
116
129 public (double localSunriseHour, double localSunriseMinute, double localSunsetHour, double localSunsetMinute, double azimuthOfSunriseDeg, double azimuthOfSunsetDeg, string status) SunriseAndSunset(double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection, double geographicalLongDeg, double geographicalLatDeg)
130 {
131 int daylightSaving = isDaylightSaving ? 1 : 0;
132
133 double localSunriseHours = PAMacros.SunriseLCT(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg);
134 double localSunsetHours = PAMacros.SunsetLCT(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg);
135
136 string sunRiseSetStatus = PAMacros.ESunRS(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg);
137
138 double adjustedSunriseHours = localSunriseHours + 0.008333;
139 double adjustedSunsetHours = localSunsetHours + 0.008333;
140
141 double azimuthOfSunriseDeg1 = PAMacros.SunriseAZ(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg);
142 double azimuthOfSunsetDeg1 = PAMacros.SunsetAZ(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg);
143
144 int localSunriseHour = sunRiseSetStatus.Equals("OK") ? PAMacros.DecimalHoursHour(adjustedSunriseHours) : 0;
145 int localSunriseMinute = sunRiseSetStatus.Equals("OK") ? PAMacros.DecimalHoursMinute(adjustedSunriseHours) : 0;
146
147 int localSunsetHour = sunRiseSetStatus.Equals("OK") ? PAMacros.DecimalHoursHour(adjustedSunsetHours) : 0;
148 int localSunsetMinute = sunRiseSetStatus.Equals("OK") ? PAMacros.DecimalHoursMinute(adjustedSunsetHours) : 0;
149
150 double azimuthOfSunriseDeg = sunRiseSetStatus.Equals("OK") ? Math.Round(azimuthOfSunriseDeg1, 2) : 0;
151 double azimuthOfSunsetDeg = sunRiseSetStatus.Equals("OK") ? Math.Round(azimuthOfSunsetDeg1, 2) : 0;
152
153 string status = sunRiseSetStatus;
154
156 }
157
176 public (double amTwilightBeginsHour, double amTwilightBeginsMin, double pmTwilightEndsHour, double pmTwilightEndsMin, string status) MorningAndEveningTwilight(double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection, double geographicalLongDeg, double geographicalLatDeg, PATwilightType twilightType)
177 {
178 int daylightSaving = isDaylightSaving ? 1 : 0;
179
180 double startOfAMTwilightHours = PAMacros.TwilightAMLCT(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg, twilightType);
181
182 double endOfPMTwilightHours = PAMacros.TwilightPMLCT(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg, twilightType);
183
184 string twilightStatus = PAMacros.ETwilight(localDay, localMonth, localYear, daylightSaving, zoneCorrection, geographicalLongDeg, geographicalLatDeg, twilightType);
185
186 double adjustedAMStartTime = startOfAMTwilightHours + 0.008333;
187 double adjustedPMStartTime = endOfPMTwilightHours + 0.008333;
188
189 double amTwilightBeginsHour = twilightStatus.Equals("OK") ? PAMacros.DecimalHoursHour(adjustedAMStartTime) : -99;
190 double amTwilightBeginsMin = twilightStatus.Equals("OK") ? PAMacros.DecimalHoursMinute(adjustedAMStartTime) : -99;
191
192 double pmTwilightEndsHour = twilightStatus.Equals("OK") ? PAMacros.DecimalHoursHour(adjustedPMStartTime) : -99;
193 double pmTwilightEndsMin = twilightStatus.Equals("OK") ? PAMacros.DecimalHoursMinute(adjustedPMStartTime) : -99;
194
195 string status = twilightStatus;
196
198 }
199
210 public (double equationOfTimeMin, double equationOfTimeSec) EquationOfTime(double gwdateDay, int gwdateMonth, int gwdateYear)
211 {
212 double sunLongitudeDeg = PAMacros.SunLong(12, 0, 0, 0, 0, gwdateDay, gwdateMonth, gwdateYear);
213 double sunRAHours = PAMacros.DecimalDegreesToDegreeHours(PAMacros.EcRA(sunLongitudeDeg, 0, 0, 0, 0, 0, gwdateDay, gwdateMonth, gwdateYear));
214 double equivalentUTHours = PAMacros.GreenwichSiderealTimeToUniversalTime(sunRAHours, 0, 0, gwdateDay, gwdateMonth, gwdateYear);
215 double equationOfTimeHours = equivalentUTHours - 12;
216
217 int equationOfTimeMin = PAMacros.DecimalHoursMinute(equationOfTimeHours);
218 double equationOfTimeSec = PAMacros.DecimalHoursSecond(equationOfTimeHours);
219
220 return (equationOfTimeMin, equationOfTimeSec);
221 }
222
230 public double SolarElongation(double raHour, double raMin, double raSec, double decDeg, double decMin, double decSec, double gwdateDay, int gwdateMonth, int gwdateYear)
231 {
232 double sunLongitudeDeg = PAMacros.SunLong(0, 0, 0, 0, 0, gwdateDay, gwdateMonth, gwdateYear);
233 double sunRAHours = PAMacros.DecimalDegreesToDegreeHours(PAMacros.EcRA(sunLongitudeDeg, 0, 0, 0, 0, 0, gwdateDay, gwdateMonth, gwdateYear));
234 double sunDecDeg = PAMacros.EcDec(sunLongitudeDeg, 0, 0, 0, 0, 0, gwdateDay, gwdateMonth, gwdateYear);
235 double solarElongationDeg = PAMacros.Angle(sunRAHours, 0, 0, sunDecDeg, 0, 0, raHour, raMin, raSec, decDeg, decMin, decSec, PAAngleMeasure.Hours);
236
237 return Math.Round(solarElongationDeg, 2);
238 }
239}
Miscellaneous macro functions supporting the other classes.
Definition PAMacros.cs:13
static double GreenwichSiderealTimeToUniversalTime(double greenwichSiderealHours, double greenwichSiderealMinutes, double greenwichSiderealSeconds, double greenwichDay, int greenwichMonth, int greenwichYear)
Convert Greenwich Sidereal Time to Universal Time.
Definition PAMacros.cs:720
static string ETwilight(double ld, int lm, int ly, int ds, int zc, double gl, double gp, PATwilightType tt)
Definition PAMacros.cs:2139
static double LocalCivilTimeGreenwichDay(double lctHours, double lctMinutes, double lctSeconds, int daylightSaving, int zoneCorrection, double localDay, int localMonth, int localYear)
Determine Greenwich Day for Local Time.
Definition PAMacros.cs:323
static double CivilDateToJulianDate(double day, double month, double year)
Convert a Greenwich Date/Civil Date (day,month,year) to Julian Date.
Definition PAMacros.cs:87
static double SunEcc(double gd, int gm, int gy)
Eccentricity of the Sun-Earth orbit.
Definition PAMacros.cs:1537
static string ESunRS(double ld, int lm, int ly, int ds, int zc, double gl, double gp)
Definition PAMacros.cs:1862
static double SunsetLCT(double ld, int lm, int ly, int ds, int zc, double gl, double gp)
Definition PAMacros.cs:1698
static double SunriseLCT(double ld, int lm, int ly, int ds, int zc, double gl, double gp)
Calculate local civil time of sunrise.
Definition PAMacros.cs:1636
static double SunELong(double gd, int gm, int gy)
Mean ecliptic longitude of the Sun at the epoch.
Definition PAMacros.cs:1507
static double TwilightPMLCT(double ld, int lm, int ly, int ds, int zc, double gl, double gp, PATwilightType tt)
Calculate evening twilight end, in local time.
Definition PAMacros.cs:2084
static double EcDec(double eld, double elm, double els, double bd, double bm, double bs, double gd, int gm, int gy)
Ecliptic - Declination (degrees)
Definition PAMacros.cs:1551
static double TwilightAMLCT(double ld, int lm, int ly, int ds, int zc, double gl, double gp, PATwilightType tt)
Calculate morning twilight start, in local time.
Definition PAMacros.cs:2028
static double DecimalDegreesToDegreeHours(double decimalDegrees)
Convert Decimal Degrees to Degree-Hours.
Definition PAMacros.cs:522
static double SunTrueAnomaly(double lch, double lcm, double lcs, int ds, int zc, double ld, int lm, int ly)
Calculate Sun's true anomaly, i.e., how much its orbit deviates from a true circle to an ellipse.
Definition PAMacros.cs:1585
static double EcRA(double eld, double elm, double els, double bd, double bm, double bs, double gd, int gm, int gy)
Ecliptic - Right Ascension (degrees)
Definition PAMacros.cs:1567
static int DecimalHoursMinute(double decimalHours)
Return the minutes part of a Decimal Hours.
Definition PAMacros.cs:55
static double DecimalHoursSecond(double decimalHours)
Return the seconds part of a Decimal Hours.
Definition PAMacros.cs:71
static double DecimalDegreesSeconds(double decimalDegrees)
Return Seconds part of Decimal Degrees.
Definition PAMacros.cs:506
static int DecimalHoursHour(double decimalHours)
Return the hour part of a Decimal Hours.
Definition PAMacros.cs:39
static double SunLong(double lch, double lcm, double lcs, int ds, int zc, double ld, int lm, int ly)
Calculate Sun's ecliptic longitude.
Definition PAMacros.cs:760
static double LocalCivilTimeToUniversalTime(double lctHours, double lctMinutes, double lctSeconds, int daylightSaving, int zoneCorrection, double localDay, int localMonth, int localYear)
Convert Local Civil Time to Universal Time.
Definition PAMacros.cs:237
static double SunPeri(double gd, int gm, int gy)
Longitude of the Sun at perigee.
Definition PAMacros.cs:1522
static double SunriseAZ(double ld, int lm, int ly, int ds, int zc, double gl, double gp)
Definition PAMacros.cs:1919
static double DecimalDegreesMinutes(double decimalDegrees)
Return Minutes part of Decimal Degrees.
Definition PAMacros.cs:490
static int LocalCivilTimeGreenwichMonth(double lctHours, double lctMinutes, double lctSeconds, int daylightSaving, int zoneCorrection, double localDay, int localMonth, int localYear)
Determine Greenwich Month for Local Time.
Definition PAMacros.cs:340
static double SunsetAZ(double ld, int lm, int ly, int ds, int zc, double gl, double gp)
Calculate azimuth of sunset.
Definition PAMacros.cs:1973
static double DecimalDegreesDegrees(double decimalDegrees)
Return Degrees part of Decimal Degrees.
Definition PAMacros.cs:474
static double Angle(double xx1, double xm1, double xs1, double dd1, double dm1, double ds1, double xx2, double xm2, double xs2, double dd2, double dm2, double ds2, PAAngleMeasure s)
Calculate the angle between two celestial objects.
Definition PAMacros.cs:2213
static int LocalCivilTimeGreenwichYear(double lctHours, double lctMinutes, double lctSeconds, int daylightSaving, int zoneCorrection, double localDay, int localMonth, int localYear)
Determine Greenwich Year for Local Time.
Definition PAMacros.cs:356
Sun calculations.
Definition PASun.cs:10
double double double double double sunDecMin
Definition PASun.cs:30
double double sunRAMin
Definition PASun.cs:30
double double double double double azimuthOfSunriseDeg
Definition PASun.cs:129
double double double double double double sunDecSec PrecisePositionOfSun(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
Definition PASun.cs:64
double double amTwilightBeginsMin
Definition PASun.cs:176
double double double double double double azimuthOfSunsetDeg
Definition PASun.cs:129
double equationOfTimeMin
Calculate the equation of time. (The difference between the real Sun time and the mean Sun time....
Definition PASun.cs:210
double double double double sunAngSizeSec SunDistanceAndAngularSize(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
Definition PASun.cs:95
double amTwilightBeginsHour
Calculate times of morning and evening twilight.
Definition PASun.cs:176
double localSunriseHour
Calculate local sunrise and sunset.
Definition PASun.cs:129
double double sunAngSizeDeg
Definition PASun.cs:95
double double double double localSunsetMinute
Definition PASun.cs:129
double double double double double double sunDecSec ApproximatePositionOfSun(double lctHours, double lctMinutes, double lctSeconds, double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection)
Definition PASun.cs:30
double double double pmTwilightEndsHour
Definition PASun.cs:176
double double double double sunDecDeg
Definition PASun.cs:30
double double double double string status MorningAndEveningTwilight(double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection, double geographicalLongDeg, double geographicalLatDeg, PATwilightType twilightType)
Definition PASun.cs:176
double double double sunRASec
Definition PASun.cs:30
double sunRAHour
Calculate approximate position of the sun for a local date and time.
Definition PASun.cs:30
double sunDistKm
Calculate distance to the Sun (in km), and angular size.
Definition PASun.cs:95
double double equationOfTimeSec EquationOfTime(double gwdateDay, int gwdateMonth, int gwdateYear)
Definition PASun.cs:210
double double localSunriseMinute
Definition PASun.cs:129
double double double double double double string status SunriseAndSunset(double localDay, int localMonth, int localYear, bool isDaylightSaving, int zoneCorrection, double geographicalLongDeg, double geographicalLatDeg)
Definition PASun.cs:129
double double double sunAngSizeMin
Definition PASun.cs:95
double double double localSunsetHour
Definition PASun.cs:129
double SolarElongation(double raHour, double raMin, double raSec, double decDeg, double decMin, double decSec, double gwdateDay, int gwdateMonth, int gwdateYear)
Calculate solar elongation for a celestial body.
Definition PASun.cs:230
double double double double pmTwilightEndsMin
Definition PASun.cs:176
PAAngleMeasure
Angle measurement units.
Definition PATypes.cs:49
PATwilightType
Twilight type.
Definition PATypes.cs:28