| 1 | #include <stdio.h>
 | 
|---|
| 2 | #include <math.h>
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "astro.h"
 | 
|---|
| 5 | 
 | 
|---|
| 6 | static void unrefractLT15 (double pr, double tr, double aa, double *ta);
 | 
|---|
| 7 | static void unrefractGE15 (double pr, double tr, double aa, double *ta);
 | 
|---|
| 8 | 
 | 
|---|
| 9 | void
 | 
|---|
| 10 | unrefract (double pr, double tr, double aa, double *ta)
 | 
|---|
| 11 | {
 | 
|---|
| 12 | #define LTLIM   14.5
 | 
|---|
| 13 | #define GELIM   15.5
 | 
|---|
| 14 | 
 | 
|---|
| 15 |         double aadeg = raddeg(aa);
 | 
|---|
| 16 | 
 | 
|---|
| 17 |         if (aadeg < LTLIM)
 | 
|---|
| 18 |             unrefractLT15 (pr, tr, aa, ta);
 | 
|---|
| 19 |         else if (aadeg >= GELIM)
 | 
|---|
| 20 |             unrefractGE15 (pr, tr, aa, ta);
 | 
|---|
| 21 |         else {
 | 
|---|
| 22 |             /* smooth blend -- important for inverse */
 | 
|---|
| 23 |             double taLT, taGE, p;
 | 
|---|
| 24 | 
 | 
|---|
| 25 |             unrefractLT15 (pr, tr, aa, &taLT);
 | 
|---|
| 26 |             unrefractGE15 (pr, tr, aa, &taGE);
 | 
|---|
| 27 |             p = (aadeg - LTLIM)/(GELIM - LTLIM);
 | 
|---|
| 28 |             *ta = taLT + (taGE - taLT)*p;
 | 
|---|
| 29 |         }
 | 
|---|
| 30 | }
 | 
|---|
| 31 | 
 | 
|---|
| 32 | static void
 | 
|---|
| 33 | unrefractGE15 (double pr, double tr, double aa, double *ta)
 | 
|---|
| 34 | {
 | 
|---|
| 35 |         double r;
 | 
|---|
| 36 |         
 | 
|---|
| 37 |         r = 7.888888e-5*pr/((273+tr)*tan(aa));
 | 
|---|
| 38 |         *ta  =  aa - r;
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | static void
 | 
|---|
| 42 | unrefractLT15 (double pr, double tr, double aa, double *ta)
 | 
|---|
| 43 | {
 | 
|---|
| 44 |         double aadeg = raddeg(aa);
 | 
|---|
| 45 |         double r, a, b;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         a = ((2e-5*aadeg+1.96e-2)*aadeg+1.594e-1)*pr;
 | 
|---|
| 48 |         b = (273+tr)*((8.45e-2*aadeg+5.05e-1)*aadeg+1);
 | 
|---|
| 49 |         r = degrad(a/b);
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         *ta  =  (aa < 0 && r < 0) ? aa : aa - r;        /* 0 below ~5 degs */
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 54 | /* correct the true altitude, ta, for refraction to the apparent altitude, aa,
 | 
|---|
| 55 |  * each in radians, given the local atmospheric pressure, pr, in mbars, and
 | 
|---|
| 56 |  * the temperature, tr, in degrees C.
 | 
|---|
| 57 |  */
 | 
|---|
| 58 | void
 | 
|---|
| 59 | refract (double pr, double tr, double ta, double *aa)
 | 
|---|
| 60 | {
 | 
|---|
| 61 | #define MAXRERR degrad(0.1/3600.)       /* desired accuracy, rads */
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         double d, t, t0, a;
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         /* first guess of error is to go backwards.
 | 
|---|
| 66 |          * make use that we know delta-apparent is always < delta-true.
 | 
|---|
| 67 |          */
 | 
|---|
| 68 |         unrefract (pr, tr, ta, &t);
 | 
|---|
| 69 |         d = 0.8*(ta - t);
 | 
|---|
| 70 |         t0 = t;
 | 
|---|
| 71 |         a = ta;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         /* use secant method to discover a value that unrefracts to ta.
 | 
|---|
| 74 |          * max=7 ave=2.4 loops in hundreds of test cases.
 | 
|---|
| 75 |          */
 | 
|---|
| 76 |         while (1) {
 | 
|---|
| 77 |             a += d;
 | 
|---|
| 78 |             unrefract (pr, tr, a, &t);
 | 
|---|
| 79 |             if (fabs(ta-t) <= MAXRERR)
 | 
|---|
| 80 |                 break;
 | 
|---|
| 81 |             d *= -(ta - t)/(t0 - t);
 | 
|---|
| 82 |             t0 = t;
 | 
|---|
| 83 |         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         *aa = a;
 | 
|---|
| 86 | 
 | 
|---|
| 87 | #undef  MAXRERR
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /* For RCS Only -- Do Not Edit */
 | 
|---|
| 91 | static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: refract.c,v $ $Date: 2008-03-25 17:45:18 $ $Revision: 1.8 $ $Name: not supported by cvs2svn $"};
 | 
|---|