2015-05-04 163 views
10

我正在寫幾種必要的方法來計算太陽在特定點的路徑。我已經使用兩種不同的來源編寫了代碼用於計算,並且都沒有產生期望的結果。來源是:http://www.pveducation.org/pvcdrom/properties-of-sunlight/suns-positionhttp://www.esrl.noaa.gov/gmd/grad/solcalc/solareqns.PDF太陽的路徑計算

注意:度到弧分是Deg * 60分。

  1. localSolartime:我已轉換的經度以「分鐘」,從localStandardTimeMeridian方法得到的當地標準時間子午線(LSTM)返回一個值,該值是在「分鐘」,並且這也是在返回的equationOfTime '分鐘'。使用pveducation的公式,我計算了時間校正,它說明了給定時間範圍內的小時間變化。當我將這個結果和localTime分別應用到當地的太陽時間(lst)方程時,結果爲676.515(此刻),這對我來說沒有任何意義。據我瞭解,當地的太陽時間表示相對於太陽的時間,當它處於天空的最高點時,在當地被認爲是太陽中午。 676.515沒有意義。有沒有人瞭解可能導致這種情況的原因。

  2. HourAngle:我希望一旦我修復localSolarTime方法,這將不需要更正。

我選擇了華盛頓哥倫比亞特區的經緯度。 Zenith和Azimuth的讀數都應該是正值,現在我的區域分別是66和201。

public class PathOfSun { 
static LocalTime localTime = LocalTime.now(); 
static double dcLat = 38.83; 
static double dcLong = -77.02; 
static DecimalFormat df = new DecimalFormat("#.0"); 

public static void main(String [] args) { 
    int day = dayOfYear(); 
    double equationOfTime = equationOfTime(day); 
    double lstm = localTimeMeridian(); 
    double lst = localSolarTime(equationOfTime, dcLong, lstm); 
    double declination = declination(day); 
    double hourAngle = hourAngle(lst); 

    double zenith = zenith(dcLat, declination, hourAngle); 
    double azimuth = azimuth(dcLong, declination, zenith, hourAngle); 

} 

//Longitude of timezone meridian 
public static double localTimeMeridian() { 
    TimeZone gmt = TimeZone.getTimeZone("GMT"); 
    TimeZone est = TimeZone.getTimeZone("EST"); 
    int td = gmt.getRawOffset() - est.getRawOffset(); 
    double localStandardTimeMeridian = 15 * (td/(1000*60*60)); //convert td to hours 
    //System.out.println("Local Time Meridian: " + localStandardTimeMeridian); 
    return localStandardTimeMeridian; 
} 

//Get the number of days since Jan. 1 
public static int dayOfYear() { 
    Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault()); 
    int dayOfYear = localCalendar.get(Calendar.DAY_OF_YEAR); 
    //System.out.println("Day: " + dayOfYear); 
    return dayOfYear; 
} 

//Emperical equation to correct the eccentricity of Earth's orbit and axial tilt 
public static double equationOfTime (double day) { 
    double d =(360.0/365.0)*(day - 81); 
    d = Math.toRadians(d); 
    double equationTime = 9.87*sin(2*d)-7.53*cos(d)-1.54*sin(d); 
    //System.out.println("Equation Of Time: " + equationTime); 
    return equationTime; 
} 
//The angle between the equator and a line drawn from the center of the Sun(degrees) 
public static double declination(int dayOfYear) { 
    double declination = 23.5*sin((Math.toRadians(360.0/365.0))*(dayOfYear - 81)); 
    //System.out.println("Declination: " + df.format(declination)); 
    return declination; 
} 

//Add the number of minutes past midnight localtime// 
public static double hourAngle(double localSolarTime) { 
    double hourAngle = 15 * (localSolarTime - 13); 
    System.out.println("Hour Angle: " + df.format(hourAngle)); //(degrees) 
    return hourAngle; 
} 

//Account for the variation within timezone - increases accuracy 
public static double localSolarTime(double equationOfTime, double longitude, double lstm) { 
    //LocalSolarTime = 4min * (longitude + localStandardTimeMeridian) + equationOfTime 
    //Time Correction is time variation within given time zone (minutes) 
    //longitude = longitude/60; //convert degrees to arcminutes 
    double localStandardTimeMeridian = lstm; 
    double timeCorrection = (4 * (longitude + localStandardTimeMeridian) + equationOfTime); 
    System.out.println("Time Correction: " + timeCorrection); //(in minutes) 
    //localSolarTime represents solar time where noon represents sun's is highest position 
    // in sky and the hour angle is 0 -- hour angle is negative in morning, and positive after solar noon. 
    double localSolarTime = (localTime.toSecondOfDay() + (timeCorrection*60)); //(seconds) 
    localSolarTime = localSolarTime/(60*60); //convert from seconds to hours 
    //Convert double to Time (HH:mm:ss) for console output 
    int hours = (int) Math.floor(localSolarTime); 
    int minutes = (int) ((localSolarTime - hours) * 60); 
    //-1 for the daylight savings 
    Time solarTime = new Time((hours-1), minutes, 0); 
    System.out.println("Local Solar Time: " + solarTime); //hours 

    return localSolarTime; 
} 

public static double azimuth(double lat, double declination, double zenith, double hourAngle) { 
    double azimuthDegree = 0; 
    double elevation = 90 - zenith; 
    elevation = Math.toRadians(elevation); 
    zenith = Math.toRadians(zenith); 
    lat = Math.toRadians(lat); 
    declination = Math.toRadians(declination); 
    hourAngle = Math.round(hourAngle); 
    hourAngle = Math.toRadians(hourAngle); 

    //double azimuthRadian = -sin(hourAngle)*cos(declination)/cos(elevation); 
    double azimuthRadian = ((sin(declination)*cos(lat)) - (cos(hourAngle)*cos(declination)* 
      sin(lat)))/cos(elevation); 

    //Account for time quadrants 
    Calendar cal = Calendar.getInstance(); 
    int hour = cal.get(Calendar.HOUR_OF_DAY); 
    if(hour > 0 && hour < 6) { 
    azimuthDegree = Math.toDegrees(acos(azimuthRadian)); 
    } 
    else if(hour >= 6 && hour < 12) { 
     azimuthDegree = Math.toDegrees(acos(azimuthRadian)); 
     azimuthDegree = 180 - azimuthDegree; 
    } else if (hour >= 12 && hour < 18) { 
     azimuthDegree = Math.toDegrees(acos(azimuthRadian)); 
     azimuthDegree = azimuthDegree - 180; 
    } else if (hour >= 18 && hour < 24) { 
     azimuthDegree = Math.toDegrees(acos(azimuthRadian)); 
     azimuthDegree = 360 - azimuthDegree; 
    } 

    System.out.println("Azimuth: " + df.format(azimuthDegree)); 
    return azimuthDegree; 
} 

public static double zenith(double lat, double declination, double hourAngle) { 
    lat = Math.toRadians(lat); 
    declination = Math.toRadians(declination); 
    hourAngle = Math.round(hourAngle); 
    hourAngle = Math.toRadians(hourAngle); 
    //Solar Zenith Angle 
    double zenith = Math.toDegrees(acos(sin(lat)*sin(declination) + (cos(lat)*cos(declination)*cos(hourAngle)))); 
    //Solar Elevation Angle 
    double elevation = Math.toDegrees(asin(sin(lat)*sin(declination) + (cos(lat)*cos(declination)*cos(hourAngle)))); 
    System.out.println("Elevation: " + df.format(elevation)); 
    System.out.println("Zenith: " + df.format(zenith)); 
    return zenith; 
} 

}

只是重申一下,這一天,當地時間經絡是完全正確的,時間和赤緯公式是準確的,但並不確切。 ----輸出更新---- new output

sensor program

----- ----- UPDATE用於 的scatterchart顯示整個白天太陽的仰角/方位角。我仍然無法找出方位輸出。這是正確的很長一段時間,但它會從增加和開始減少(〜270 - > 0)。一旦我最終獲得輸出權限,我一定會更新代碼。

+1

難以理解確切的問題是由於措辭。你說它應該產生幾分鐘,然後在幾分鐘內給出結果;爲什麼??你說這兩個應該是積極的,然後給出積極的結果;爲什麼??你有沒有調試過這個來關聯輸入和輸出? – ChiefTwoPencils

+0

您的zenith()和azimuth()函數不能反映您提供的引用中給出的公式。你有沒有試圖簡化它們? – BadZen

+0

此外,請注意,您將不得不調整結果[0,2 * Pi]以獲得「標準化」(非負)角度。 – BadZen

回答

3

您將經度作爲度數傳遞給localSolarTime(),然後將其除以60,並聲明這是爲了將其轉換爲弧的分鐘數。這是錯誤的;你以後的計算需要度數,即使你需要幾分鐘的弧度,你也會乘以60,而不是分割。

這個錯誤的劃分結果是-1.3 °的經度,並且當你找到當地時間子午線和你的位置之間的角度時,你會得到一個很大的角度(大約75 °)。它應該是一個小角度,一般爲±7.5 °。大角度會導致大量的時間校正,並拋出所有的東西。


更新:azimuth()方法的更新版本,象限選擇應基於太陽,或者,等同的時角,對本地太陽時,而不是標準的牆上時鐘時間。而且,在所有計算中使用的小時角度不應該四捨五入。而不是測試的四個不同象限,該方法看起來是這樣的:

public static double azimuth(double lat, double declination, double zenith, double hourAngle) 
{ 
    double elevation = Math.toRadians(90 - zenith); 
    lat = Math.toRadians(lat); 
    declination = Math.toRadians(declination); 
    hourAngle = Math.toRadians(hourAngle); 
    double azimuthRadian = acos(((sin(declination) * cos(lat)) - (cos(hourAngle) * cos(declination) * sin(lat)))/cos(elevation)); 
    double azimuthDegree = Math.toDegrees(azimuthRadian); 
    if (hourAngle > 0) 
    azimuthDegree = 360 - azimuthDegree; 
    System.out.println("Azimuth: " + df.format(azimuthDegree)); 
    return azimuthDegree; 
} 

最後,你是在爲azimuth()方法的lat參數傳遞dcLong;這應該是dcLat

我建議在整個內部使用弧度,並且只對輸入和輸出進行轉換和轉換。這將有助於防止錯誤,並減少舍入誤差和不必要的混亂。

+0

謝謝埃裏克森指出。我會調整代碼,並希望它會產生一些更愉快的東西! – wellington

+0

我已經解決了這個問題,現在方法正常工作,除了最後的計算 - 方位角是不正確的。早晨,方位角的範圍在下午(12-24小時)爲0-180(從0-12)到180-360。鑑於此,我需要調整acos()中的值以考慮單位圓中每個象限的符號變化。我試過使用兩個不同的公式,都來自http://en.wikipedia.org/wiki/Solar_azimuth_angle,但第二個方程的輸出產生'?'。第一個方程式非常接近,但與實際情況相差約10度。 – wellington

+0

@wellington如果您需要其他幫助,請使用您當前的方位角方法以及樣本輸入和預期結果爲您的帖子添加更新。 – erickson