Cpp Code Sun Position

Embed Size (px)

Citation preview

  • 8/18/2019 Cpp Code Sun Position

    1/4

    // C++ program calculating the sunrise and sunset for// the current date and a fixed location(latitude,longitude)// Jarmo Lammi 1999 - 2000// Last update January 6th, 2000

    #include #include #include

    #define pi 3.14159

    //extern double pi;double tpi = 2 * pi;double degs = 180.0/pi;double rads = pi/180.0;

    double L,g,daylen;double SunDia = 0.53; // Sunradius degrees

    double AirRefr = 34.0/60.0; // athmospheric refraction degrees //

    // Get the days to J2000// h is UT in decimal hours// FNday only works between 1901 to 2099 - see Meeus chapter 7

    double FNday (int y, int m, int d, float h) {int luku = - 7 * (y + (m + 9)/12)/4 + 275*m/9 + d;// type casting necessary on PC DOS and TClite to avoid overflowluku+= (long int)y*367;return (double)luku - 730531.5 + h/24.0;};

    // the function below returns an angle in the range// 0 to 2*pi

    double FNrange (double x) {  double b = x / tpi;

      double a = tpi * (b - (long)(b));  if (a < 0) a = tpi + a;  return a;};

    // Calculating the hourangle//double f0(double lat, double declin) {double fo,dfo;// Correction: different sign at S HSdfo = rads*(0.5*SunDia + AirRefr); if (lat < 0.0) dfo = -dfo;fo = tan(declin + dfo) * tan(lat*rads);if (fo>0.99999) fo=1.0; // to avoid overflow //

    fo = asin(fo) + pi/2.0;return fo;};

    // Calculating the hourangle for twilight times//double f1(double lat, double declin) {double fi,df1;// Correction: different sign at S HSdf1 = rads * 6.0; if (lat < 0.0) df1 = -df1;

  • 8/18/2019 Cpp Code Sun Position

    2/4

    fi = tan(declin + df1) * tan(lat*rads);if (fi>0.99999) fi=1.0; // to avoid overflow //fi = asin(fi) + pi/2.0;return fi;};

    // Find the ecliptic longitude of the Sun

    double FNsun (double d) {

    // mean longitude of the Sun

    L = FNrange(280.461 * rads + .9856474 * rads * d);

    // mean anomaly of the Sun

    g = FNrange(357.528 * rads + .9856003 * rads * d);

    // Ecliptic longitude of the Sun

    return FNrange(L + 1.915 * rads * sin(g) + .02 * rads * sin(2 * g));};

    // Display decimal hours in hours and minutesvoid showhrmn(double dhr) {int hr,mn;hr=(int) dhr;mn = (dhr - (double) hr)*60;if (hr < 10) cout

  • 8/18/2019 Cpp Code Sun Position

    3/4

    double tzone=2.0;cout > latit;cin >> longit;cin >> tzone;

    // testing// m=6; day=10;

    double d = FNday(y, m, day, h);

    // Use FNsun to find the ecliptic longitude of the// Sun

    double lambda = FNsun(d);

    // Obliquity of the ecliptic

    double obliq = 23.439 * rads - .0000004 * rads * d;

    // Find the RA and DEC of the Sun

    double alpha = atan2(cos(obliq) * sin(lambda), cos(lambda));double delta = asin(sin(obliq) * sin(lambda));

    // Find the Equation of Time// in minutes// Correction suggested by David Smithdouble LL = L - alpha;if (L < pi) LL += tpi;double equation = 1440.0 * (1.0 - LL / tpi);double ha = f0(latit,delta);double hb = f1(latit,delta);double twx = hb - ha; // length of twilight in radianstwx = 12.0*twx/pi; // length of twilight in hourscout

  • 8/18/2019 Cpp Code Sun Position

    4/4

    cout