File: jdn.c

package info (click to toggle)
iraf-rvsao 2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,456 kB
  • sloc: ansic: 963; lisp: 651; fortran: 397; makefile: 27
file content (341 lines) | stat: -rw-r--r-- 9,390 bytes parent folder | 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
static char SccsID[] = "@(#)jdn.c	2.2\t11/29/99";
/* Routines to return Julian day from various forms.
 *
 * CREATION 06/29/87 Bill Wyatt
 */

#include <stdio.h>
#include <sys/time.h>
#include <math.h>

#include "trig.h"
#include "poly.h"

#define ABS(x) ((x) < 0 ? -(x) : (x))

void doxperror();

/* NOTE: UT 1 Jan 1970, 0000 hours (midnight) == 2440587.5 Julian date, 
 * and Ultrix time is kept in seconds (and, supposedly,  microseconds)
 *  of UT from UT 1 Jan 1970, 0000 hours.
 */
#define JD1970 2440587.5

/* INPUT is a time value structure. OUTPUT is the JDN (to a fraction
 * of a second. The fraction is limited by the true accuracy
 * of the system time (currently about 1/60th sec).
 */
 
double
jdn_tval(tp)
     struct timeval *tp;
{
    return(JD1970 + (((double)(tp->tv_sec))/8.6400e+4) + 
           (((double)(tp->tv_usec))/8.64e+10));
}

/* Input a Julian Day Number, return a ptr to a tm structure 
 * as in gmtime(3).
 */
struct tm *
tm_jdn(jdn)
     double jdn;
{
    struct tm *gmtime();
    double rint();
    long ut = rint((jdn - JD1970) * 86400.0);

    return(gmtime(&ut));
}

/* Input a JDN, return a timeval pointer, number of secs since 1970.0 
 * Return is negative if JDN is before 1970.0.
 */
tval_jdn(jdn, tval)
     double jdn;
     struct timeval *tval;
{
    if(jdn < JD1970) return(-1);
    tval->tv_sec  = (jdn - JD1970) * 86400.0;
    tval->tv_usec = rint(((jdn - JD1970) * 86400.0 - tval->tv_sec) * 1.e6);
    return(0);
}

/* Input is a Julian Day Number. Output is an ASCII string giving UT time
 * in the form as given by asctime(3).
 */
char *
ut_jdn(jdn)
     double jdn;
{
    struct tm *gmtime();
    char *asctime();

    long ut = (jdn - JD1970) * 86400.0 + 0.5;
    return(asctime(gmtime(&ut)));
}

/* OUTPUT the current JDN */
double 
jdn_current()
{
    int gettimeofday();
    struct timeval tp;
    struct timezone tz;
    double jdn_tval();

    if(gettimeofday(&tp, &tz) == -1) {
        doxperror("jdn_now");
        return(0.0);
    }
    return(jdn_tval(&tp));
}

/* convert from GMT Year, Month, Day, Hours, Minutes, Seconds to JDN.
 * 
 * ******* ONLY VALID FROM JAN 0, 1900 through FEB 28th, 2100 ***********
 *     (because no century non-leap-year corrections are made)
 * 
 * N.B.: midnight on Jan 0, 1900 is Julian day 2415019.5 
 */
static unsigned short daynum[] = {
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 
  };

double
jdn_date(year, month, day, hour, min, sec, usec)
     int year;  /* since 1900 (i.e. 0 to 99 for 20th cent. */
     int month; /* range is 0 to 11 */
     int day;   /* range is 1 to 31  - no limit checking */
     int hour;  /* range 0 to 23 */
     int min, sec;  /* ranges 0 to 59 */
     int usec;  /* microseconds (because Unix gives it to us! */
{
    int ndays;

    if(month < 0 || month > 11) month = 0;
    ndays = (year * 365) + (year / 4) + daynum[month] + day -
      		(((year % 4) == 0 && month < 2 && year > 0) ? 1 : 0);
    
    return(2415019.5 + (double)ndays + 
           (((double)((hour * 3600) + (min * 60) + sec) + 
             (((double)usec) / 1.e+6)) / 86400.0));
}

/* INPUT is in `struct tm' form as defined in <sys/time.h>. The number of
 * microseconds is passed separately since the tm structure doesn't
 * hold it.
 */    
double
jdn_tmdate(tmp, usecs)
     struct tm *tmp;
     int usecs;
{
    double jdn_date();

    return(jdn_date(tmp->tm_year, tmp->tm_mon, 
                    tmp->tm_mday, tmp->tm_hour,
                    tmp->tm_min, tmp->tm_sec, usecs));
}

/* return solar ecliptic longitude in RADIANS */

#ifdef oldcode
static double 
	TU1[] = { 0.000303, 36000.768920, 279.69668 },
	TU2[] = { -3.0e-6, -1.50e-4, 35999.04975, 358.47583 };

struct spoly 
	tu1 = { 3, TU1, 0.0, 1.0 },
	tu2 = { 4, TU2, 0.0, 1.0 };

double
sun3long(jdn, msun, masun)
     double jdn;
     double *msun;   /* RETURN mean long. in RADIANS if non-NULL */
     double *masun;  /* RETURN mean anomaly in RADIANS if non-NULL */
{
    double evalpoly(), fmod();
        /* tu == Julian centuries from 0 Jan 1900 12:00 noon */
    double tu = (jdn - 2415020.0) / 36525.0; 
    double mlsun, ma, lsun;

    mlsun = fmod(evalpoly(&tu1, tu), 360.0) - 0.005; /* mean long. in deg. */
    ma    = fmod(evalpoly(&tu2, tu), 360.0);    /* mean anomaly */
    lsun = (sin(DEGTORAD(2.0 * ma)) * 0.020) +
      (sin(DEGTORAD(ma)) * 1.916) + mlsun;

    if(msun != (double *)NULL)  *msun  = DEGTORAD(mlsun);
    if(masun != (double *)NULL) *masun = DEGTORAD(ma);
    return(DEGTORAD(lsun));
}
#endif
double
sun3long(jdn, msun, masun)
     double jdn;
     double *msun;   /* RETURN mean long. in RADIANS if non-NULL */
     double *masun;  /* RETURN mean anomaly in RADIANS if non-NULL */
{
    double fmod();
    /* days from 0 Jan 1900 12:00 noon */
    double Du = (jdn - 2415020.0) / 10000.0;
    double du = jdn - 2415020.0;
    double mlsun, ma, lsun;

    /* mean long. in deg. */
    mlsun =
      fmod(279.696678 + 0.9856473354 * du + 2.267e-5 * Du * Du, 360.0);

    /* mean anomaly */
    ma    = 
      fmod(358.475833 + 0.9856002670 * du - 
	   Du * (1.12e-5 * Du + 7.e-8 * Du * Du), 360.0);

    lsun = (sin(DEGTORAD(2.0 * ma)) * 0.020) +
      (sin(DEGTORAD(ma)) * 1.916) + mlsun;

    if(msun != (double *)NULL)  *msun  = DEGTORAD(mlsun);
    if(masun != (double *)NULL) *masun = DEGTORAD(ma);
    return(DEGTORAD(lsun));
}

double
sunlong(jdn)
     double jdn;
{
    double sun3long();

    return(sun3long(jdn, (double *)NULL, (double *)NULL));
}


/* static double secliptic = 0.3978166;  /* sine and cosine of obliquity  */
/* static double cecliptic = 0.9174650;  /* of ecliptic (23 deg. 27 min.) */

/* make correction for geocentric Julian day to heliocentric Julian day */
double 
hjdn(gjdn, rastar, decstar)
     double gjdn;     /* topocentric Julian Day */
     double rastar,
            decstar;  /* coordinates in RADIANS */
{
    void  obl1950_();
    double sunlong();
    double slong = sunlong(gjdn);  /* solar ecliptic longitude in RADIANS */
    double ecliptic;
    double ca = cos(rastar),
    	   sa = sin(rastar);
    double cd = cos(decstar), 
    	   sd = sin(decstar);
    double cls = cos(slong), 
    	   sls = sin(slong);
    double cm = cos(0.0172021242 * (gjdn - 2442416.01329));
    double delta;

    obl1950_(&gjdn, &ecliptic);
    delta = -0.0057755 *  (1.0 - 0.01673 * cm) *
      ((cls * ca * cd) + (sls * ((sin(ecliptic) * sd) + 
                                 (cos(ecliptic) * cd * sa))));
    return(gjdn + delta);
}

double 
hjdn_iraf(gjdn, rastar, decstar)
     double gjdn;     /* topocentric Julian Day */
     double rastar,
            decstar;  /* coordinates in RADIANS */
{
    double tanom, rsun, delta;
    double starlat, starlong;
    double slong;
    double ecliptic;
    double fmod();
    void  obl1950_(), ast_coord();

    double t = (gjdn - 2415020.0) / 36525.0;
    double manom = 358.47583 +
      t * (35999.04975 - t * (0.000150 + t * 0.000003));
    double lperi = 101.22083 +
      t * (1.7191733 + t * (0.000453 + t * 0.000003));
    double eccen = 0.01675104 - 
      t * (0.00004180 + t * 0.000000126);

    manom = DEGTORAD(fmod(manom, 360.0));
    lperi = DEGTORAD(fmod(lperi, 360.0));
    
    /* true anomaly, radians */
    tanom = manom + 
      eccen * (2. - 0.25 * eccen * eccen) * sin(manom) +
	1.25 * eccen * eccen * sin(2. * manom) +
	  13./12. * eccen * eccen * eccen * sin(3. * manom);

    /* solar ecliptic longitude in RADIANS */
    slong = lperi + tanom + M_PI;

    obl1950_(&gjdn, &ecliptic);

    ast_coord(0., 0., -(M_PI_2), M_PI_2 - ecliptic,
	      rastar, decstar, &starlong, &starlat);
    rsun = (1.0 - eccen * eccen) / (1. + eccen * cos(tanom));

    delta = -0.00577 * rsun * cos(starlat) * cos(starlong - slong);
    return(gjdn + delta);
}

/* AST_COORD -- Convert spherical coordinates to new system.
 *
 * This procedure converts the longitude-latitude coordinates (a1, b1)
 * of a point on a sphere into corresponding coordinates (a2, b2) in a
 * different coordinate system that is specified by the coordinates of its
 * origin (ao, bo).  The range of a2 will be from -pi to pi.
 */
void
ast_coord (ao, bo, ap, bp, a1, b1, a2, b2)
     double  ao, bo;         /* Origin of new coordinates (radians) */
     double  ap, bp;         /* Pole of new coordinates (radians)   */
     double  a1, b1;         /* Coordinates to be converted (radians)*/
     double  *a2, *b2;         /* Converted coordinates (radians)     */
{ 
    double  sao, cao, sbo, cbo, sbp, cbp;
    double  x, y, z, xp, yp, zp, temp;

    x = cos (a1) * cos (b1);
    y = sin (a1) * cos (b1);
    z = sin (b1);
    xp = cos (ap) * cos (bp);
    yp = sin (ap) * cos (bp);
    zp = sin (bp);
 
    /* Rotate the origin about z. */
    sao = sin (ao);
    cao = cos (ao);
    sbo = sin (bo);
    cbo = cos (bo);
    temp = -xp * sao + yp * cao;
    xp = xp * cao + yp * sao;
    yp = temp;
    temp = -x * sao + y * cao;
    x = x * cao + y * sao;
    y = temp;

    /* Rotate the origin about y. */
    temp = -xp * sbo + zp * cbo;
    xp = xp * cbo + zp * sbo;
    zp = temp;
    temp = -x * sbo + z * cbo;
    x = x * cbo + z * sbo;
    z = temp;
 
    /* Rotate pole around x. */
    sbp = zp;
    cbp = yp;
    temp = y * cbp + z * sbp;;
    y = y * sbp - z * cbp;
    z = temp;
 
    /* Final angular coordinates. */
    *a2 = atan2 (y, x);
    *b2 = asin (z);
    return;
}