Package: astronomical-almanac / 5.6-7

conjunct.c.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
Description: move changes in source files to patches
Author: Thorsten Alteholz <debian@alteholz.de>
Last-Update: 03 Mar 2012
Forwarded: patch is from upstream

Index: astronomical-almanach/conjunct.c
===================================================================
--- astronomical-almanach.orig/conjunct.c	2012-03-02 12:01:46.000000000 +0100
+++ astronomical-almanach/conjunct.c	2012-03-02 12:27:49.000000000 +0100
@@ -1,12 +1,19 @@
-/* Search program to find dates of equinox or new moon.  */
+/* Search program to find dates of equinox or new/full moon.  */
+
+double STARTDATE;
+double ENDDATE;
 
-#define STARTDATE  2439935.5
-#define ENDDATE    2445000.5
-/* Define one of these nonzero, the others zero.  */
 #define NEWMOON 0
 #define FULLMOON 1
-#define EQUINOX 0
-
+#define SPRING 2
+#define SUMMER 3
+#define AUTUMN 4
+#define WINTER 5
+
+#define _XOPEN_SOURCE /* glibc2 needs this to declare strptime */
+#include <stdlib.h>
+#include <getopt.h>
+#include <time.h>
 #include "kep.h"
 /* Conversion factors between degrees and radians */
 double DTR = 1.7453292519943295769e-2;
@@ -16,6 +23,9 @@
 double PI = 3.14159265358979323846;
 double TPI = 6.28318530717958647693;
 
+/* unused, but must be defined for rplanet.c and rstar.c */
+struct orbit *elobject;	/* pointer to orbital elements of object */
+
 extern double PI;
 
 /* Standard epochs.  Note Julian epochs (J) are measured in
@@ -115,131 +125,255 @@
 double dradt, ddecdt;
 extern double FAR moonpol[];
 extern double FAR moonpp[];
+static int event=NEWMOON;
 
 double zgetdate(), gethms();
 double func(), search();
 int apparent();
 
-int main()
+int main(int argc, char **argv)
 {
-  double t, t0;
+  double t=0, t0;
+  int c;
+  time_t now=time(NULL);
+  struct tm date=*localtime(&now);
 
   kinit();
+
+  STARTDATE=caltoj( date.tm_year+1900, date.tm_mon+1, date.tm_mday );
+  ENDDATE=caltoj( date.tm_year+1900+1, date.tm_mon+1, date.tm_mday );
+
+  while (1) {
+    int option_index = 0;
+    static struct option long_options[] = {
+      {"start", 1, 0, 's'},
+      {"end", 1, 0, 'e'},
+      {"spring", 0, 0, 'V'},
+      {"vernal", 0, 0, 'V'},
+      {"summer", 0, 0, 'S'},
+      {"autumn", 0, 0, 'A'},
+      {"fall", 0, 0, 'A'},
+      {"winter", 0, 0, 'W'},
+      {"newmoon", 0, 0, 'N'},
+      {"fullmoon", 0, 0, 'F'},
+      {"help", 0, 0, 'h'},
+      {0, 0, 0, 0}
+    };
+    c = getopt_long (argc, argv, "s:e:VSAWNFh",
+		     long_options, &option_index);
+    if (c == -1)
+      break;
+    switch (c) {
+    case 's': 
+      if (strptime(optarg, "%Y-%m-%d", &date))
+	STARTDATE=caltoj( date.tm_year+1900, date.tm_mon+1, date.tm_mday );
+      else
+	STARTDATE=atof(optarg); 
+      break;
+    case 'e':
+      if (strptime(optarg, "%Y-%m-%d", &date))
+	ENDDATE=caltoj( date.tm_year+1900, date.tm_mon+1, date.tm_mday );
+      else
+	ENDDATE=atof(optarg); 
+      break;
+    case 'V': event=SPRING; break;
+    case 'S': event=SUMMER; break;
+    case 'A': event=AUTUMN; break;
+    case 'W': event=WINTER; break;
+    case 'N': event=NEWMOON; break;
+    case 'F': event=FULLMOON; break;
+    case 'h': 
+    default:
+      printf("conjunct -  find dates of equinox or new/full moon\n"
+	     "options:\n"
+	     " -s, --start DATE  Julian or ISO 8601 starting date (default today)\n"
+	     " -e, --end DATE  ending date (default 1 year from today)\n"
+	     " -V, --vernal    find times of spring equinox\n"
+	     " -S, --summer    find times of summer solstice\n"
+	     " -A, --autumn    find times of autumn equinox\n"
+	     " -W, --winter    find times of winter solstice\n"
+	     " -F, --fullmoon  find times of full moon\n"
+	     " -N, --newmoon   find times of new moon (default)\n");
+      exit(0);		     
+      break;
+    }
+  }
+  if (STARTDATE >= ENDDATE)
+    {
+      printf("start date=%f >= end date=%f\n", STARTDATE, ENDDATE);
+      exit(1);
+    }
+  
   objnum = 0;
-  t0 = STARTDATE;
-  while( t0 <= ENDDATE )
+  //  printf("184:STARTDATE=%6.1f\n", STARTDATE);
+  t0 = STARTDATE - (((event==FULLMOON)||(event==NEWMOON))?40:400);
+  while( 1 )
     {
       prtflg = 0;
-#if EQUINOX
-      t = search( t0, 0.0, 364.0 );
-#endif
-#if NEWMOON
-      t = search( t0, PI, 27.0 );
-#endif
-#if FULLMOON
-      t = search( t0, 0.0, 27.0 );
-#endif
+      switch(event)
+	{
+	case SPRING:
+	  t = search( t0, 0.0, 364.0 );
+	  break;
+	case AUTUMN:
+	  t = search( t0, PI, 364.0 );
+	  break;
+	case WINTER:
+	  t = search( t0, -PI/2, 364.0 );
+	  break;
+	case SUMMER:
+	  t = search( t0, PI/2, 364.0 );
+	  break;
+	case NEWMOON:
+	  t = search( t0, 0.0, 27.0 );
+	  break;
+	case FULLMOON:
+	  t = search( t0, PI, 27.0 );
+	  break;
+	}
       TDT = t;
-      printf("%.4f ", t);
-      prtflg = 1;
-      jtocal(t);
+      if (t > ENDDATE)
+	break;
+      if (t >= STARTDATE)
+	{
+	  printf("%.4f ", t);
+	  prtflg = 1;
+	  jtocal(t);
+	}
       t0 = t;
     }
 exit(0);
 }
 
 /* Search for desired conjunction.
-   Step forward DELTA days from date T.  Then begin searching
-   for the date at which the desired function equals Y.  */
+   On the first call to this function, search forward from date T
+   in steps of DELTA / 8 days until the error changes sign.
+   On subsequent calls, step forward DELTA days from T, then search forward
+   in steps of 1 day until the error changes sign.
+   Then, in both cases, reduce the error by interval halving
+   until the function equals Y with the desired precision.  */
+
+static int first_search = 0;
+
 double search(t, y, delta)
-double t, y, delta;
+     double t, y, delta;
 {
-  double tl, tm, th, yl, ym, yh, e;
+  double tl, tm, th, yl, ym, yh, el, eh, em, dt;
 
-  tl = t;
-  yl = func(t);
-  th = tl + delta;
-  yh = yl;
+  if (first_search == 0)
+    {
+      dt = 0.125 * delta;
+      first_search = 1;
+      th = t;
+    }
+  else
+    {
+      dt = 1.0;
+      th = t + delta;
+    }
+  yh = func(th);
+  eh = yh - y;
+  if (eh > PI)
+    eh -= TPI;
+  if (eh <= -PI)
+    eh += TPI;
   /* Bracket the solution date.  */
-  do
+  for (;;)
     {
       tl = th;
       yl = yh;
-      th = th + 1.0;
+      el = eh;
+      th = th + dt;
       yh = func(th);
-      e = yh - y;
-    if(e > PI)
-      {
-	e -= TPI;
-	yh -= TPI;
-      }
-    if(e < -PI)
-      {
-	e += TPI;
-	yh += TPI;
-      }
+      eh = yh - y;
+      if(eh > PI)
+	eh -= TPI;
+      if (eh <= -PI)
+	eh += TPI;
+      /* Bracket the solution date.  */
+      for (;;)
+	{
+	  tl = th;
+	  yl = yh;
+	  el = eh;
+	  th = th + dt;
+	  yh = func(th);
+	  eh = yh - y;
+	  if(eh > PI)
+	    eh -= TPI;
+	  if(eh <= -PI)
+	    eh += TPI;
+	  /* Keep searching while the error is large.  */
+	  if (fabs (eh) > 0.5*PI)
+	    continue;
+	  /* Look for sign change */
+	  if((el * eh) <= 0.0)
+	    break;
+	}
+      /* Search by simple interval halving.  */
+      while( (th - tl) > .00001 )
+	{
+	  tm = 0.5 * (tl + th);
+	  ym = func(tm);
+	  em = ym - y;
+	  if(em > PI)
+	    em -= TPI;
+	  if(em <= -PI)
+	    em += TPI;
+	  /* Replace the inteval boundary that has the same sign as em.  */
+	  if( em * eh > 0 )
+	    {
+	      yh = ym;
+	      th = tm;
+	      eh = em;
+	    }
+	  else
+	    {
+	      yl = ym;
+	      tl = tm;
+	      el = em;
+	    }
+	}
+      tm = tl + (th - tl) * (-el)/(yh -yl);
+      return (tm);
     }
-while(e < 0);
-
-/* Search by simple interval halving.  */
-while( (th - tl) > .00001 )
-  {
-    tm = 0.5 * (tl + th);
-    ym = func(tm);
-    e = ym - y;
-    if(e > PI)
-      {
-	e -= TPI;
-	ym -= TPI;
-      }
-    if(e < -PI)
-      {
-	e += TPI;
-	ym += TPI;
-      }
-    if( e > 0 )
-      {
-	yh = ym;
-	th = tm;
-      }
-    else
-      {
-	yl = ym;
-	tl = tm;
-      }
-  }
-tm = tl + (th - tl) * (y - yl)/(yh - yl);
-return (tm);
 }
 
-/* Compute desired relation of apperent ecliptic longitude
+/* Compute desired relation of apparent ecliptic longitude
    as a function of the ephemeris date.  */
 double func(t)
 double t;
 {
   double p[3], q[3], polar[3];
   double s;
-#if NEWMOON || FULLMOON
   double m;
-#endif
+  double val;
 
   TDT = t;
   /* Longitude of the sun.  */
   objnum = 0;
   apparent(p,q);
   lonlat(p,TDT,polar,0);
-  s = polar[0];
-#if NEWMOON || FULLMOON
-  /* Longitude of the moon.  */
-  objnum = 3;
-  apparent(p,q);
-  lonlat(p,TDT,polar,0);
-  m = polar[0];
-  return (s - m);
-#endif
-#if EQUINOX
-  return s;
-#endif
+  val = s = polar[0];
+  switch(event)
+    {
+    case NEWMOON:
+    case FULLMOON:
+      /* Longitude of the moon.  */
+      objnum = 3;
+      apparent(p,q);
+      lonlat(p,TDT,polar,0);
+      m = polar[0];
+      val = s - m;
+      break;
+    case SPRING:
+    case WINTER:
+    case AUTUMN:
+    case SUMMER:
+      val = s;
+      break;
+    }
+  return val;
 }
 
 
@@ -319,7 +453,7 @@
  */
 precess( p, TDT, -1 );
 
-/* Ajust for nutation
+/* Adjust for nutation
  * at current ecliptic.
  */
 epsiln( TDT );