File: sunzenith.c

package info (click to toggle)
wmweather+ 2.15-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,100 kB
  • ctags: 579
  • sloc: ansic: 5,822; sh: 1,294; makefile: 42
file content (138 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download | duplicates (6)
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
#include "config.h"

/*  Copyright (C) 2002  Brad Jorsch <anomie@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Algorithms from http://www.srrb.noaa.gov/highlights/sunrise/azel.html */

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>

#include "convert.h"

/*  Purpose: calculate the Geometric Mean Longitude of the Sun (degrees) */
double calcGeomMeanLongSun(double t) {
    double L0 = 280.46646 + t * (36000.76983 + 0.0003032 * t);
    while(L0 > 360.0) {
        L0 -= 360.0;
    }
    while(L0 < 0.0) {
        L0 += 360.0;
    }
    return L0;
}


/*  Purpose: calculate the Geometric Mean Anomaly of the Sun (degrees) */
double calcGeomMeanAnomalySun(double t) {
    return 357.52911 + t * (35999.05029 - 0.0001537 * t);
}


/*  Purpose: calculate the eccentricity of earth's orbit */
double calcEccentricityEarthOrbit(double t) {
    return 0.016708634 - t * (0.000042037 + 0.0000001267 * t);
}


/*  Purpose: calculate the equation of center for the sun (degrees) */
double calcSunEqOfCenter(double t) {
    double m = deg2rad(calcGeomMeanAnomalySun(t));

    return sin(m) * (1.914602 - t * (0.004817 + 0.000014 * t)) + sin(m+m) * (0.019993 - 0.000101 * t) + sin(m+m+m) * 0.000289;
}


/*  Purpose: calculate the true longitude of the sun (degrees) */
double calcSunTrueLong(double t) {
    return calcGeomMeanLongSun(t) + calcSunEqOfCenter(t);
}


/*  Purpose: calculate the apparent longitude of the sun (degrees) */
double calcSunApparentLong(double t) {
    return calcSunTrueLong(t) - 0.00569 - 0.00478 * sin(deg2rad(125.04-1934.136*t));
}


/*  Purpose: calculate the mean obliquity of the ecliptic (degrees) */
double calcMeanObliquityOfEcliptic(double t) {
    return 23.0 + (26.0 + ((21.448 - t*(46.8150 + t*(0.00059 - t*(0.001813))))/60.0))/60.0;
}


/*  Purpose: calculate the corrected obliquity of the ecliptic (degrees) */
double calcObliquityCorrection(double t) {
    return calcMeanObliquityOfEcliptic(t) + 0.00256*cos(deg2rad(125.04-1934.136*t));
}


/*  Purpose: calculate the declination of the sun (degrees) */
double calcSunDeclination(double t) {
    return rad2deg(asin(sin(deg2rad(calcObliquityCorrection(t))) *
                         sin(deg2rad(calcSunApparentLong(t)))));
}


/*  Purpose: calculate the difference between true solar time and mean
 *              solar time (minutes)
 */
double calcEquationOfTime(double t) {
    double l0 = deg2rad(calcGeomMeanLongSun(t));
    double e = calcEccentricityEarthOrbit(t);
    double m = deg2rad(calcGeomMeanAnomalySun(t));
    double y = tan(deg2rad(calcObliquityCorrection(t))/2.0);
    double sinm = sin(m);

    y *= y;

    return rad2deg(y*sin(2.0*l0) - 2.0*e*sinm + 4.0*e*y*sinm*cos(2.0*l0)
                    - 0.5*y*y*sin(4.0*l0) - 1.25*e*e*sin(2.0*m))*4.0;
}


double calcSolarZenith(double latitude, double longitude, int year, int month, int day, int timeUTC){
    double T, trueSolarTime, hourAngle, solarDec, csz, zenith, exoatmElevation, te, refractionCorrection;

    T=jd2jcent(mdy2jd(year, month, day) + timeUTC/1440.0);
    trueSolarTime = timeUTC + calcEquationOfTime(T) - 4.0 * longitude;
    hourAngle = trueSolarTime / 4.0 - 180.0;
    solarDec = calcSunDeclination(T);
    csz = sin(deg2rad(latitude)) * sin(deg2rad(solarDec)) +
        cos(deg2rad(latitude)) * cos(deg2rad(solarDec)) *
        cos(deg2rad(hourAngle));
    zenith=rad2deg(acos(csz));
    exoatmElevation = 90.0 - zenith;
    if (exoatmElevation > 85.0) {
        refractionCorrection = 0.0;
    } else {
        te = tan(deg2rad(exoatmElevation));
        if (exoatmElevation > 5.0) {
            refractionCorrection = 58.1/te - 0.07/(te*te*te) +
                0.000086/(te*te*te*te*te);
        } else if (exoatmElevation > -0.575) {
            refractionCorrection = 1735.0 + exoatmElevation*(-518.2 + exoatmElevation*(103.4 + exoatmElevation*(-12.79 + exoatmElevation*0.711)));
        } else {
            refractionCorrection = -20.774 / te;
        }
        refractionCorrection = refractionCorrection / 3600.0;
    }
    return zenith - refractionCorrection;
}