0

As a beginner in java, I am trying some simple case. As from this SO post and 2nd link to the pdf file given there, I have developed this code:

import java.time.LocalTime;
import java.util.Calendar;
import static java.lang.Math.acos;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.tan;


/**
 * Created by rudra on 17/02/18.
 */

public class PathOfSun {

    static public double getTime() {
        LocalTime loacltime = LocalTime.now();//.getHour();
        int tzone = 5; //TODO: placeholder, will be calculated
        //    int localhour = time.getHour();
        int hour = loacltime.getHour();
        int min = loacltime.getMinute();
        int sec = loacltime.getSecond();
        double lat  =  latlang.Lat;
        double lang = latlang.Lang;
        Calendar calendar = Calendar.getInstance();
        int dayofyear = calendar.get(Calendar.DAY_OF_YEAR);
        int daysthisyear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
        double pi = Math.PI;

        double gamma = (2 * pi / daysthisyear) * (dayofyear - 1 + (hour - 12) / 24);
        double eqtime = 229.18 * (0.000075 + 0.001868 * cos(gamma) - 0.032077 * sin(gamma) -
                0.014615 * cos(2 * gamma) - 0.04849 * sin(2 * gamma));
        double decl = 0.006918 - 0.399912 * cos(gamma) + 0.070257 * sin(gamma) - 0.006758 * cos(2 * gamma)
                + 0.000907 * sin(2 * gamma) - 0.002697 * cos(3 * gamma) + 0.00148 * sin(3 * gamma);
        double toffset = eqtime + 4 * lang + 60 * tzone;
        double tst = 60 * hour + min + sec / 60 + toffset;
        double ha = tst / 4 - 180;

        // Zenith
        double phi = acos(sin(lat) * sin(decl) + cos(lat) * cos(decl) * cos(ha));
        // Azimuth
        double theta = 180 - acos((sin(lat) * cos(phi) - sin(decl)) / (cos(lat) * sin(phi)));

        // Calculating the hourAngle as in second page
        // At sunrise
        double ha2 = acos(cos(90.8*180/pi)/cos(lat)*cos(decl)-tan(lat)*tan(decl));
        double stime = 720 - 4 * (lang - ha2) - eqtime;
        System.out.println("Time of Sun "+ stime);
        return stime;
    }

which, when called as:

    PathOfSun.getTime();

is giving wrong time, means, the stime for sunset and sunrise (+/- ha2) is giving almost same value.

Is there any mistake in my code? Please kindly help me doing this correct.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
BaRud
  • 3,055
  • 7
  • 41
  • 89

1 Answers1

0

My suggestion,

take a look at the Sunrise and Sunset special cases in the given link from NOAA you have provided in the post.

The sunrise and sunset hours are special cases because of the refraction of the Sun and Earth's atmosphere. So those special case may need to be accounted so that the returned time for sunrise and sunset are correct.

Another special case hour you might want to think about is the noon time hour. Is the program accounting this and the returned time is correct with the given time by NOAA clock? The link shows the like of times in different time zones.

As a proverb and a famous saying for most programmers including myself are familiar with and struggle with, "the devil is in the details". In this case the special case or edge cases of the problem.

Any questions and/or clarification, leave a comment and will do my best.