sunwait
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
/*
** sunriset.c - computes Sun rise/set times, including twilights
** Written as DAYLEN.C, 1989-08-16
** Modified to SUNRISET.C, 1992-12-01
** (c) Paul Schlyter, 1989, 1992
** Released to the public domain by Paul Schlyter, December 1992
*/

/*
**
** Who  When        Ver  What
** IFC  04-12-2014  0.5  General fixes to "sunwait for windows" and linux port
** IFC  08-12-2014  0.6  Add timezone for output of timings
** IFC  29-04-2015  0.7  Fix for timezone (esp near date line) and more debug
** IFC  2015-05-27  0.8  Resolve 'dodgy day' and cleanup
**
*/

#include <stdio.h>
#include <stdlib.h> // Linux
#include <iostream>
#include <math.h>
#include <time.h>

#include "sunwait.h"
#include "sunriset.h"

using namespace std;

/************************************************************************/
/* Note: Eastern longitude positive, Western longitude negative         */
/*       Northern latitude positive, Southern latitude negative         */
/*                                                                      */
/* >>>   Longitude value IS critical in this function!              <<< */
/*                                                                      */
/*       days  = Days since 2000 plus fraction to local noon            */
/*       altit = the altitude which the Sun should cross                */
/*               Set to -35/60 degrees for rise/set, -6 degrees         */
/*               for civil, -12 degrees for nautical and -18            */
/*               degrees for astronomical twilight.                     */
/*       upper_limb: non-zero -> upper limb, zero -> center             */
/*               Set to non-zero (e.g. 1) when computing rise/set       */
/*               times, and to zero when computing start/end of         */
/*               twilight.                                              */
/*                                                                      */
/*               Both times are relative to the specified altitude,     */
/*               and thus this function can be used to comupte          */
/*               various twilight times, as well as rise/set times      */
/* Return Codes:                                                        */
/*       0  = sun rises/sets this day. Success.                         */
/*                    Times held at *trise and *tset.                   */
/*       +1 = Midnight Sun. Fail.                                       */
/*                    Sun above the specified "horizon" all 24 hours.   */
/*                    *trise set to time when the sun is at south,      */
/*                    minus 12 hours while *tset is set to the south    */
/*                    time plus 12 hours. "Day" length = 24 hours       */
/*       -1 = Polar Night. Fail.                                        */
/*                    Sun is below the specified "horizon" all 24hours. */
/*                    "Day" length = 0 hours, *trise and *tset are      */
/*                    both set to the time when the sun is at south.    */
/*                                                                      */
/************************************************************************/
void sunriset (const runStruct *pRun, targetStruct *pTarget)
{
  double sr;               /* solar distance, astronomical units */
  double sra;              /* sun's right ascension */
  double sdec;             /* sun's declination */
  double sradius;          /* sun's apparent radius */
  double siderealTime;     /* local sidereal time */
  double altitude;         /* sun's altitude: angle to the sun relative to the mathematical (flat-earth) horizon */
  double diurnalArc = 0.0; /* the diurnal arc, hours */
  double southHour  = 0.0; /* Hour UTC the sun is directly south (or north for southern Hemisphere) of lat/long position */

  /* compute sideral time at 00:00 UTC of target day for this longitude. */
  siderealTime = revolution (GMST0(pTarget->daysSince2000) + 180.0 + pRun->longitude); // 180 = 0 hour UTC is measured 180 degrees from dateline

  /* compute sun's ra + decl at this moment */
  sun_RA_dec (pTarget->daysSince2000, &sra, &sdec, &sr );

  /* compute time when sun is directly south - in hours UTC. "12.00" == noon. "15" == 180degrees/12hours [degrees per hour] */
  southHour = 12.0 - rev180 (siderealTime - sra)/15.0;

  /* compute the sun's apparent radius, degrees */
  sradius = 0.2666 / sr;  // Apparent angular radius of sun is 0.2666/distance in AU (deg)

  /* Do correction for upper limb ('top' of sun) only, for "daylight" sunrise or set. Otherwise calculate for centre of sun */
  if (pTarget->twilightAngle == TWILIGHT_ANGLE_DAYLIGHT)
    altitude = pTarget->twilightAngle - sradius;
  else
    altitude = pTarget->twilightAngle;

  /* compute the diurnal arc that the sun traverses to reach the specified altitide altit: */
  double cost = (sind(altitude) - sind(pRun->latitude) * sind(sdec)) / (cosd(pRun->latitude) * cosd(sdec));

  if (abs(int(cost)) < 1.0)
    diurnalArc = 2*acosd(cost)/15.0;    /* Diurnal arc, hours */
  else if (cost>=1.0)
    diurnalArc =  0.0; // Polar Night
  else
    diurnalArc = 24.0; // Midnight Sun

  if (pRun->debug == ONOFF_ON)
  { printf ("Debug: sunriset.cpp: Sun directly south: %f UTC, Diurnal Arc = %f hours\n", southHour, diurnalArc);
    printf ("Debug: sunriset.cpp: Days since 2000: %li\n", pTarget->daysSince2000);
    if (diurnalArc >= 24.0) printf ("Debug: sunriset.cpp: No rise or set: Midnight Sun\n");
    if (diurnalArc <=  0.0) printf ("Debug: sunriset.cpp: No rise or set: Polar Night\n");
  }

  // Error Check - just make sure odd things don't happen (causing trouble further on)
  if (diurnalArc > 24.0) diurnalArc = 24.0;
  if (diurnalArc <  0.0) diurnalArc =  0.0;

  /* Apply values */
  pTarget->southHourUTC = southHour;
  pTarget->diurnalArc   = diurnalArc;
}

void sunpos (const double d, double *lon, double *r)
/******************************************************/
/* Computes the Sun's ecliptic longitude and distance */
/* at an instant given in d, number of days since     */
/* 2000 Jan 0.0.  The Sun's ecliptic latitude is not  */
/* computed, since it's always very near 0.           */
/******************************************************/
{
  double M,         /* Mean anomaly of the Sun */
         w,         /* Mean longitude of perihelion */
                    /* Note: Sun's mean longitude = M + w */
         e,         /* Eccentricity of Earth's orbit */
         E,         /* Eccentric anomaly */
         x, y,      /* x, y coordinates in orbit */
         v;         /* True anomaly */

  /* Compute mean elements */
  M = revolution (356.0470 + 0.9856002585 * d);
  w = 282.9404 + 4.70935E-5 * d;
  e = 0.016709 - 1.151E-9 * d;

  /* Compute true longitude and radius vector */
  E = M + e * RADIAN_TO_DEGREE * sind(M) * (1.0 + e * cosd(M));
  x = cosd (E) - e;
  y = sqrt (1.0 - e*e) * sind(E);
  *r = sqrt (x*x + y*y);              /* Solar distance */
  v = atan2d (y, x);                  /* True anomaly */
  *lon = revolution (v + w);          /* True solar longitude, made 0..360 degrees */
}

void sun_RA_dec (const double d, double *RA, double *dec, double *r)
{
  double lon, obl_ecl;
  double xs, ys, zs;
  double xe, ye, ze;
  
  /* Compute Sun's ecliptical coordinates */
  sunpos (d, &lon, r);
  
  /* Compute ecliptic rectangular coordinates */
  xs = *r * cosd(lon);
  ys = *r * sind(lon);
  zs = 0; /* because the Sun is always in the ecliptic plane! */

  /* Compute obliquity of ecliptic (inclination of Earth's axis) */
  obl_ecl = 23.4393 - 3.563E-7 * d;
  
  /* Convert to equatorial rectangular coordinates - x is unchanged */
  xe = xs;
  ye = ys * cosd(obl_ecl);
  ze = ys * sind(obl_ecl);
  
  /* Convert to spherical coordinates */
  *RA = atan2d(ye, xe);
  *dec = atan2d(ze, sqrt(xe*xe + ye*ye));
}

//
// Utility functions
//

// Reduce angle to within 0..359.999 degrees
double revolution (const double x)
{ double remainder = fmod (x, (double) 360.0);
  return remainder < (double) 0.0 ? remainder + (double) 360.0 : remainder;
}

// Reduce angle to -179.999 to +180 degrees
double rev180 (const double x)
{ double y = revolution (x);
  return y <= (double) 180.0 ? y : y - (double) 360.0;
}

// Fix angle to 0-359.999
double fixLongitude (const double x)
{ return revolution (x);
}

// Fix angle to 0-89.999 and -0.001 to -89.999
double fixLatitude (const double x)
{
  // Make angle 0 to 359.9999
  double y = revolution (x);

       if (y <= (double)  90.0) ;
  else if (y <= (double) 180.0) y = (double) 180.0 - y;
  else if (y <= (double) 270.0) y = (double) 180.0 - y;
  else if (y <= (double) 360.0) y = y - (double) 360.0;

  // Linux compile of sunwait doesn't like 90, Windows is OK. 
  // Let's just wiggle things a little bit to make things OK.
       if (y == (double)  90.0) y = (double)  89.9999999;
  else if (y == (double) -90.0) y = (double) -89.9999999;

  return y;
}

// Time must be between 0:00 amd 23:59
double fix24 (const double x)
{
  double remainder = fmod (x, (double) 24.0);
  return remainder < (double) 0.0 ? remainder + (double) 24.0 : remainder;
}

/*******************************************************************/
/* This function computes GMST0, the Greenwhich Mean Sidereal Time */
/* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at  */
/* 0h UT).  GMST is then the sidereal time at Greenwich at any     */
/* time of the day.  I've generalized GMST0 as well, and define it */
/* as:  GMST0 = GMST - UT  --  this allows GMST0 to be computed at */
/* other times than 0h UT as well.  While this sounds somewhat     */
/* contradictory, it is very practical:  instead of computing      */
/* GMST like:                                                      */
/*                                                                 */
/*  GMST = (GMST0) + UT * (366.2422/365.2422)                      */
/*                                                                 */
/* where (GMST0) is the GMST last time UT was 0 hours, one simply  */
/* computes:                                                       */
/*                                                                 */
/*  GMST = GMST0 + UT                                              */
/*                                                                 */
/* where GMST0 is the GMST "at 0h UT" but at the current moment!   */
/* Defined in this way, GMST0 will increase with about 4 min a     */
/* day.  It also happens that GMST0 (in degrees, 1 hr = 15 degr)   */
/* is equal to the Sun's mean longitude plus/minus 180 degrees!    */
/* (if we neglect aberration, which amounts to 20 seconds of arc   */
/* or 1.33 seconds of time)                                        */
/*                                                                 */
/*******************************************************************/

inline double GMST0 (const double d)
{
  /* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr  */
  /* L = M + w, as defined in sunpos().  Since I'm too lazy to */
  /* add these numbers, I'll let the C compiler do it for me.  */
  /* Any decent C compiler will add the constants at compile   */
  /* time, imposing no runtime or code overhead.               */
  return revolution ((180.0 + 356.0470 + 282.9404) + (0.9856002585 + 4.70935E-5) * d);
}


unsigned long daysSince2000 (const time_t *pTimet)
{
  struct tm tmpTm;

  myUtcTime (pTimet, &tmpTm);
  
  unsigned int yearsSince2000 = tmpTm.tm_year - 100; // Get year, but tm_year starts from 1900

  // Calucate number of leap days, but -
  //   yearsSince2000 - 1 
  // Don't include this year as tm_yday includes this year's leap day in the next bit

  unsigned int leapDaysSince2000
    = (unsigned int) floor ((yearsSince2000-1)/4)    // Every evenly divisible 4 years is a leap-year
    - (unsigned int) floor ((yearsSince2000-1)/100)  // Except centuries
    + (unsigned int) floor ((yearsSince2000-1)/400)  // Unless evenlt divisible by 400
    + 1;                                             // 2000 itself was a leap year with the 400 rule (a fix for 0/400 == 0)

  return (yearsSince2000 * 365) + leapDaysSince2000 + tmpTm.tm_yday;
}

/*
** Utility functions
*/

long   myRound (const double d) { return d > 0.0 ? (int) (d + 0.5) : (int) (d - 0.5) ; }
long   myTrunc (const double d) { return (d>0) ? (int) floor(d) : (int) ceil(d) ; }
double myAbs   (const double d) { return (d>0) ? d : -d ; }

int hours   (const double d) { return myTrunc (d); }
int minutes (const double d) { return myTrunc (fmod (myAbs (d) * 60,   60)); }