source: Sophya/trunk/SophyaExt/XephemAstroLib/marsmoon.c@ 2820

Last change on this file since 2820 was 2818, checked in by cmv, 20 years ago

Update de Xephem 3.7 cmv 21/08/2005

File size: 6.2 KB
Line 
1/* mars moon info */
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <math.h>
7
8#include "astro.h"
9#include "bdl.h"
10
11static int use_bdl (double JD, char *dir, MoonData md[M_NMOONS]);
12static void moonradec (double msize, MoonData md[M_NMOONS]);
13static void moonSVis (Obj *sop, Obj *mop, MoonData md[M_NMOONS]);
14static void moonEVis (MoonData md[M_NMOONS]);
15static void moonPShad (Obj *sop, Obj *mop, MoonData md[M_NMOONS]);
16static void moonTrans (MoonData md[M_NMOONS]);
17
18/* moon table and a few other goodies and when it was last computed */
19static double mdmjd = -123456;
20static MoonData mmd[M_NMOONS] = {
21 {"Mars", NULL},
22 {"Phobos", "I"},
23 {"Deimos", "II"},
24};
25static double sizemjd;
26
27/* file containing BDL coefficients */
28static char mbdlfn[] = "mars.9910";
29
30/* These values are from the Explanatory Supplement.
31 * Precession degrades them gradually over time.
32 */
33#define POLE_RA degrad(317.61)
34#define POLE_DEC degrad(52.85)
35
36
37/* get mars info in md[0], moon info in md[1..M_NMOONS-1].
38 * if !dir always use bruton model.
39 * if !mop caller just wants md[] for names
40 * N.B. we assume sop and mop are updated.
41 */
42void
43marsm_data (
44double Mjd, /* mjd */
45char dir[], /* dir in which to look for helper files */
46Obj *sop, /* Sun */
47Obj *mop, /* mars */
48double *sizep, /* mars's angular diam, rads */
49double *polera, double *poledec,/* pole location */
50MoonData md[M_NMOONS]) /* return info */
51{
52 double JD, dmag;
53
54 /* always copy back at least for name */
55 memcpy (md, mmd, sizeof(mmd));
56
57 /* pole */
58 if (polera) *polera = POLE_RA;
59 if (poledec) *poledec = POLE_DEC;
60
61 /* nothing else if repeat call or just want names */
62 if (Mjd == mdmjd || !mop) {
63 if (mop) {
64 *sizep = sizemjd;
65 }
66 return;
67 }
68 JD = Mjd + MJD0;
69
70 /* planet in [0] */
71 md[0].ra = mop->s_ra;
72 md[0].dec = mop->s_dec;
73 md[0].mag = get_mag(mop);
74 md[0].x = 0;
75 md[0].y = 0;
76 md[0].z = 0;
77 md[0].evis = 1;
78 md[0].svis = 1;
79
80 /* size is straight from mop */
81 *sizep = degrad(mop->s_size/3600.0);
82
83 /* from Pasachoff/Menzel: brightest @ .6 AU */
84 dmag = 5.0*log10(mop->s_edist + 0.4);
85 md[1].mag = 11.8 + dmag;
86 md[2].mag = 12.9 + dmag;
87
88 /* get moon x,y,z from BDL if possible */
89 if (dir && use_bdl (JD, dir, md) < 0) {
90 int i;
91 for (i = 1; i < M_NMOONS; i++)
92 md[i].x = md[i].y = md[i].z = 0.0;
93 fprintf (stderr, "No mars model available\n");
94 }
95
96 /* set visibilities */
97 moonSVis (sop, mop, md);
98 moonPShad (sop, mop, md);
99 moonEVis (md);
100 moonTrans (md);
101
102 /* fill in moon ra and dec */
103 moonradec (*sizep, md);
104
105 /* save */
106 mdmjd = Mjd;
107 sizemjd = *sizep;
108 memcpy (mmd, md, sizeof(mmd));
109}
110
111/* hunt for BDL file in dir[] and use if possible.
112 * return 0 if ok, else -1
113 */
114static int
115use_bdl (
116double JD, /* julian date */
117char dir[], /* directory */
118MoonData md[M_NMOONS]) /* fill md[1..NM-1].x/y/z for each moon */
119{
120#define MRAU .00002269 /* Mars radius, AU */
121 double x[M_NMOONS], y[M_NMOONS], z[M_NMOONS];
122 char buf[1024];
123 FILE *fp;
124 int i;
125
126 /* only valid 1999 through 2010 */
127 if (JD < 2451179.50000 || JD >= 2455562.5)
128 return (-1);
129
130 /* open */
131 (void) sprintf (buf, "%s/%s", dir, mbdlfn);
132 fp = fopen (buf, "r");
133 if (!fp)
134 return (-1);
135
136 /* use it */
137 if ((i = read_bdl (fp, JD, x, y, z, buf)) < 0) {
138 fprintf (stderr, "%s: %s\n", mbdlfn, buf);
139 fclose (fp);
140 return (-1);
141 }
142 if (i != M_NMOONS-1) {
143 fprintf (stderr, "%s: BDL says %d moons, code expects %d", mbdlfn,
144 i, M_NMOONS-1);
145 fclose (fp);
146 return (-1);
147 }
148
149 /* copy into md[1..NM-1] with our scale and sign conventions */
150 for (i = 1; i < M_NMOONS; i++) {
151 md[i].x = x[i-1]/MRAU; /* we want mars radii +E */
152 md[i].y = -y[i-1]/MRAU; /* we want mars radii +S */
153 md[i].z = -z[i-1]/MRAU; /* we want mars radii +front */
154 }
155
156 /* ok */
157 fclose (fp);
158 return (0);
159}
160
161/* given mars loc in md[0].ra/dec and size, and location of each moon in
162 * md[1..NM-1].x/y in mars radii,find ra/dec of each moon in md[1..NM-1].ra/dec.
163 */
164static void
165moonradec (
166double msize, /* mars diameter, rads */
167MoonData md[M_NMOONS]) /* fill in RA and Dec */
168{
169 double mrad = msize/2;
170 double mra = md[0].ra;
171 double mdec = md[0].dec;
172 int i;
173
174 for (i = 1; i < M_NMOONS; i++) {
175 double dra = mrad * md[i].x;
176 double ddec = mrad * md[i].y;
177 md[i].ra = mra + dra;
178 md[i].dec = mdec - ddec;
179 }
180}
181
182/* set svis according to whether moon is in sun light */
183static void
184moonSVis(
185Obj *sop, /* SUN */
186Obj *mop, /* mars */
187MoonData md[M_NMOONS])
188{
189 double esd = sop->s_edist;
190 double eod = mop->s_edist;
191 double sod = mop->s_sdist;
192 double soa = degrad(mop->s_elong);
193 double esa = asin(esd*sin(soa)/sod);
194 double h = sod*mop->s_hlat;
195 double nod = h*(1./eod - 1./sod);
196 double sca = cos(esa), ssa = sin(esa);
197 int i;
198
199 for (i = 1; i < M_NMOONS; i++) {
200 MoonData *mdp = &md[i];
201 double xp = sca*mdp->x + ssa*mdp->z;
202 double yp = mdp->y;
203 double zp = -ssa*mdp->x + sca*mdp->z;
204 double ca = cos(nod), sa = sin(nod);
205 double xpp = xp;
206 double ypp = ca*yp - sa*zp;
207 double zpp = sa*yp + ca*zp;
208 int outside = xpp*xpp + ypp*ypp > 1.0;
209 int infront = zpp > 0.0;
210 mdp->svis = outside || infront;
211 }
212}
213
214/* set evis according to whether moon is geometrically visible from earth */
215static void
216moonEVis (MoonData md[M_NMOONS])
217{
218 int i;
219
220 for (i = 1; i < M_NMOONS; i++) {
221 MoonData *mdp = &md[i];
222 int outside = mdp->x*mdp->x + mdp->y*mdp->y > 1.0;
223 int infront = mdp->z > 0.0;
224 mdp->evis = outside || infront;
225 }
226}
227
228/* set pshad and sx,sy shadow info */
229static void
230moonPShad(
231Obj *sop, /* SUN */
232Obj *mop, /* mars */
233MoonData md[M_NMOONS])
234{
235 int i;
236
237 for (i = 1; i < M_NMOONS; i++) {
238 MoonData *mdp = &md[i];
239 mdp->pshad = !plshadow (mop, sop, POLE_RA, POLE_DEC, mdp->x,
240 mdp->y, mdp->z, &mdp->sx, &mdp->sy);
241 }
242}
243
244/* set whether moons are transiting */
245static void
246moonTrans (MoonData md[M_NMOONS])
247{
248 int i;
249
250 for (i = 1; i < M_NMOONS; i++) {
251 MoonData *mdp = &md[i];
252 mdp->trans = mdp->z > 0 && mdp->x*mdp->x + mdp->y*mdp->y < 1;
253 }
254}
255
256
257/* For RCS Only -- Do Not Edit */
258static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: marsmoon.c,v $ $Date: 2005-08-21 10:02:38 $ $Revision: 1.3 $ $Name: not supported by cvs2svn $"};
Note: See TracBrowser for help on using the repository browser.