【发布时间】:2013-10-03 17:30:38
【问题描述】:
这是Josh r code 的 php 实现,用于计算给定日期和时间的太阳位置:
这是 MvG 帮助后的更正代码:
function getSunPosition($lat, $long, $year, $month, $day, $hour, $min) {
// From https://stackoverflow.com/questions/8708048/position-of-the-sun-given-time-of-day-latitude-and-longitude?rq=1
// Get Julian date for date at noon
$jd = gregoriantojd($month,$day,$year);
//correct for half-day offset
$dayfrac = $hour / 24 - .5;
//now set the fraction of a day
$frac = $dayfrac + $min / 60 / 24;
$jd = $jd + $frac;
// The input to the Atronomer's almanach is the difference between
// the Julian date and JD 2451545.0 (noon, 1 January 2000)
$time = ($jd - 2451545);
// Ecliptic coordinates
// Mean longitude
$mnlong = (280.460 + 0.9856474 * $time);
$mnlong = fmod($mnlong,360);
if ($mnlong < 0) $mnlong = ($mnlong + 360);
// Mean anomaly
$mnanom = (357.528 + 0.9856003 * $time);
$mnanom = fmod($mnanom,360);
if ($mnanom < 0) $mnanom = ($mnanom + 360);
$mnanom = deg2rad($mnanom);
// Ecliptic longitude and obliquity of ecliptic
$eclong = ($mnlong + 1.915 * sin($mnanom) + 0.020 * sin(2 * $mnanom));
$eclong = fmod($eclong,360);
if ($eclong < 0) $eclong = ($eclong + 360);
$oblqec = (23.439 - 0.0000004 * $time);
$eclong = deg2rad($eclong);
$oblqec = deg2rad($oblqec);
// Celestial coordinates
// Right ascension and declination
$num = (cos($oblqec) * sin($eclong));
$den = (cos($eclong));
$ra = (atan($num / $den));
if ($den < 0) $ra = ($ra + pi());
if ($den >= 0 && $num <0) $ra = ($ra + 2*pi());
$dec = (asin(sin($oblqec) * sin($eclong)));
// Local coordinates
// Greenwich mean sidereal time
//$h = $hour + $min / 60 + $sec / 3600;
$h = $hour + $min / 60;
$gmst = (6.697375 + .0657098242 * $time + $h);
$gmst = fmod($gmst,24);
if ($gmst < 0) $gmst = ($gmst + 24);
// Local mean sidereal time
$lmst = ($gmst + $long / 15);
$lmst = fmod($lmst,24);
if ($lmst < 0) $lmst = ($lmst + 24);
$lmst = deg2rad($lmst * 15);
// Hour angle
$ha = ($lmst - $ra);
if ($ha < pi()) $ha = ($ha + 2*pi());
if ($ha > pi()) $ha = ($ha - 2*pi());
// Latitude to radians
$lat = deg2rad($lat);
// Azimuth and elevation
$el = (asin(sin($dec) * sin($lat) + cos($dec) * cos($lat) * cos($ha)));
$az = (asin(-cos($dec) * sin($ha) / cos($el)));
// For logic and names, see Spencer, J.W. 1989. Solar Energy. 42(4):353
if ((sin($dec) - sin($el) * sin($lat)) >00) {
if(sin($az) < 0) $az = ($az + 2*pi());
} else {
$az = (pi() - $az);
}
$el = rad2deg($el);
$az = rad2deg($az);
$lat = rad2deg($lat);
return array(number_format($el,2),number_format($az,2));
}
已在刚果(赤道附近)经纬度测试:-4.77867 / 11.86364,日期为 2013 年 9 月 1 日 10:00。在这种情况下,正确答案是: 海拔 = 67.77503 方位角 = 54.51532
感谢您帮助调试此 php 代码!
格雷格·法布尔。
【问题讨论】:
-
这不是唯一的问题,而是在您引用的示例的第 52 行 (
$h = $hour + $min / 60 + sec / 3600;) 和未定义的常量sec- 我从原始猜测您应该有一个参数 $sec或者它可以设置为零? -
感谢 daniel,我复制/粘贴代码时打错了。我删除了秒数,因为它对我来说太精确了。我更正了代码,但我仍在寻找(数学?)错误!
-
比较你的代码和 R 代码之间的所有中间结果,找出第一个区别。
-
我会支持@MvG 的建议。在 R 中,您可以通过以下方式单步执行代码: (1) 读取
sunPosition的定义; (2) 做debugonce(sunPosition);然后 (3) 致电sunPosition()获取示例位置。与debug或debugonce的 php 等效项并行执行相同操作,只需几分钟即可找到结果不同的地方。祝你好运! -
here 是您的示例位置的 the R code by Josh O'Brien 运行。代码被扩充以打印沿途的所有作业。我注意到结果与您声称的不同;我读了仰角 67.77503 和方位角 54.51532。引用 R 代码的结果时是否不小心交换了结果变量?
标签: php r math astronomy azimuth