Skip to content

Commit

Permalink
Merge pull request #1 from minube/dev
Browse files Browse the repository at this point in the history
Dev - Solar-Position library ready
  • Loading branch information
abmo committed Aug 31, 2017
2 parents a58475d + da0b96f commit b2cc16c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
composer.lock
vendor/
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ More details about the different sun position phases in [timeanddate.com](https:
* UTC [ DateTime](http:https://php.net/manual/es/class.datetime.php) object

## Example
```php
use SolarPosition\Calculator;

$latitude = 40.433202;
$longitude = -3.690161;
$date = array('day'=>23, 'month'=>6, 'year'=>2017);
$sun = new SolarPosition();

$sun = new Calculator();
$sunsetTime = $sun->getSunset($date, $latitude,$longitude);
print_r($sunsetTime);

/*
DateTime Object
(
[date] => 2017-06-23 19:48:35
[timezone_type] => 3
[timezone] => UTC
)

*/
```
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "solar-position",
"description": "Minube Solar Position PHP Library",
"autoload": {
"psr-4": {
"SolarPosition\\": "src/SolarPosition/"
}
},
"config": {
"vendor-dir": "vendor"
}
}
90 changes: 48 additions & 42 deletions SolarPosition.php → src/SolarPosition/Calculator.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

/**
* www.minube.com
* User: abmo
*/
class SolarPosition
namespace SolarPosition;

use DateTime;
use DateTimeZone;

class Calculator
{
const CIVIL_DEPRESSION = 6.0;
const NAUTICAL_DEPRESSION = 12.0;
Expand All @@ -19,7 +20,7 @@ class SolarPosition
const SUNRISE_DEPRESSION = 0.833;
const TIME_UNIT = 60.0;
const DAY_TIME_UNIT = 24;

/**
* Calculate timezone offset days from timezone text identifier to UTC as reference (i.e. "Europe/Amsterdam")
* @param string $timeZoneText
Expand All @@ -36,7 +37,7 @@ protected function calculateTimeZoneOffset($timeZoneText = "")
}
return $offset;
}

/**
* Calculate julian day from date (day, month, year) and timezone
* @param array $date
Expand All @@ -49,21 +50,21 @@ public function calculateJulianDay($date, $timeZoneText = null)
$month = $date['month'];
$year = $date['year'];
$day += $this->calculateTimeZoneOffset($timeZoneText);

if ($month <= 2) {
$year -= 1;
$month += 12;
}

$julianDay = floor(365.25 * ($year + 4716)) + floor(30.6001 * ($month + 1)) + $day - 1524.5;

if ($julianDay > self::MAX_JULIAN_DAY) {
$tmpYear = floor($year / 100.0);
$julianDay += 2 - $tmpYear + floor($tmpYear / 4.0);
}
return $julianDay;
}

/**
* Calculate Julian Day from Julian Century value
* @param $julianCentury
Expand All @@ -73,7 +74,7 @@ protected function julianCenturyToJulianDay($julianCentury)
{
return ($julianCentury * self::MULTIPLIER_JULIAN_CENTURY) + self::OFFSET_JULIAN_CENTURY;
}

/**
* Calculate Julian Century from Julian Day value
* @param $julianDay
Expand All @@ -83,7 +84,7 @@ protected function julianDayToJulianCentury($julianDay)
{
return ($julianDay - self::OFFSET_JULIAN_CENTURY) / self::MULTIPLIER_JULIAN_CENTURY;
}

/**
* Calculate mean obliquity of ecliptic (term used by astronomers for the inclination of Earth's equator
* with respect to the ecliptic) from Julian Century
Expand All @@ -94,9 +95,9 @@ protected function calcMeanObliquityOfEcliptic($julianCentury)
{
$seconds = 21.448 - $julianCentury * (46.8150 + ($julianCentury * (0.00059 - $julianCentury * (0.001813))));
return 23.0 + ((26.0 + ($seconds / self::TIME_UNIT)) / self::TIME_UNIT);

}

/**
* Corrected obliquity in degrees
* @param $julianCentury
Expand All @@ -108,7 +109,7 @@ protected function calcObliquityCorrection($julianCentury)
$obliquityInDegrees = $this->calcMeanObliquityOfEcliptic($julianCentury) + (0.00256 * cos(deg2Rad($omega)));
return $obliquityInDegrees;
}

/**
* Calculate mean anomaly of the sun position
* @param $julianCentury
Expand All @@ -118,7 +119,7 @@ protected function calcGeomMeanAnomaly($julianCentury)
{
return 357.52911 + $julianCentury * (35999.05029 - 0.0001537 * $julianCentury);
}

/**
* Geometric mean longitude of sun position
* @param $julianCentury
Expand All @@ -130,7 +131,7 @@ protected function calcGeomMeanLongitude($julianCentury)
$geomLong = $geomLong % 360;
return $geomLong;
}

/**
* Eccentricity Earth Orbit calculation
* @param $julianCentury
Expand All @@ -140,7 +141,7 @@ protected function calcEccentricityEarthOrbit($julianCentury)
{
return 0.016708634 - $julianCentury * (0.000042037 + 0.0000001267 * $julianCentury);
}

/**
* Equation of Time calculation in minutes of time
* @param $julianCentury
Expand All @@ -154,19 +155,19 @@ protected function calcEquationOfTime($julianCentury)
$meanAnomaly = $this->calcGeomMeanAnomaly($julianCentury);
$obliquity = tan(deg2Rad($this->calcObliquityCorrection($julianCentury)) / 2.0);
$obliquity *= $obliquity;

$sin2MeanLong = sin(2.0 * $meanLongInRad);
$sinMeanAnomaly = sin(deg2Rad($meanAnomaly));
$cos2MeanLong = cos(2.0 * $meanLongInRad);
$sin4MeanLong = sin(4.0 * $meanLongInRad);
$sin2MeanAnomaly = sin(2.0 * deg2Rad($meanAnomaly));

$equationOfTime = $obliquity * $sin2MeanLong - 2.0 * $eccentricity * $sinMeanAnomaly +
4.0 * $eccentricity * $obliquity * $sinMeanAnomaly * $cos2MeanLong -
0.5 * $obliquity * $obliquity * $sin4MeanLong - 1.25 * $eccentricity * $eccentricity * $sin2MeanAnomaly;
return rad2Deg($equationOfTime) * 4.0;
}

/**
* Equation of the center in degrees
* @param $julianCentury
Expand All @@ -183,7 +184,7 @@ protected function calcEquationOfCenter($julianCentury)
$sin2MeanAnomaly * (0.019993 - 0.000101 * $julianCentury) + $sin3MeanAnomaly * 0.000289;
return $equationOfCenter;
}

/**
* Calculate true (ecliptic) longitude of the sun in degrees
* @param $julianCentury
Expand All @@ -195,7 +196,7 @@ protected function calcTrueLongitude($julianCentury)
$equationOfCenter = $this->calcEquationOfCenter($julianCentury);
return $meanLongitude + $equationOfCenter;
}

/**
* Calculate apparent logintude of the sun in degrees
* @param $julianCentury
Expand All @@ -207,7 +208,7 @@ protected function calcApparentLongitude($julianCentury)
$omega = 125.04 - 1934.136 * $julianCentury;
return $trueLongitude - 0.00569 - 0.00478 * sin(deg2Rad($omega));
}

/**
* Sun declination angle in degrees
* @param $julianCentury
Expand All @@ -221,7 +222,7 @@ protected function calcDeclination($julianCentury)
$theta = rad2Deg(asin($sinT));
return $theta;
}

/**
* Hour angle calculation
* @param $latitude
Expand All @@ -242,7 +243,7 @@ protected function calcHourAngle($latitude, $solarDec, $solarDepression)
(cos($latitudeInRad) * cos($solarDecInRad))) - (tan($latitudeInRad) * tan($solarDecInRad))));
return $hourAngle * $negativeFlag;
}

/**
* Create UTC DateTime object
* @param $timeUTC
Expand All @@ -256,19 +257,19 @@ protected function getUtcDateTime($timeUTC, $date)
$hour = (int)$timeUTC;
$minute = (int)(($timeUTC - $hour) * self::TIME_UNIT);
$second = (int)(((($timeUTC - $hour) * self::TIME_UNIT) - $minute) * self::TIME_UNIT);

$minute += (int)floor($second / self::TIME_UNIT);
$second -= (int)(self::TIME_UNIT * floor($second / self::TIME_UNIT));
$hour += (int)floor($minute / self::TIME_UNIT);
$minute -= (int)(self::TIME_UNIT * floor($minute / self::TIME_UNIT));
$date['day'] += (int)floor($hour / self::DAY_TIME_UNIT);
$hour -= (int)(self::DAY_TIME_UNIT * floor($hour / self::DAY_TIME_UNIT));

$dateTimeUtc->setDate($date['year'], $date['month'], $date['day']);
$dateTimeUtc->setTime($hour, $minute, $second);
return $dateTimeUtc;
}

/**
* UTC time calculation
* @param $julianCentury
Expand All @@ -287,7 +288,7 @@ protected function calculateUtcTime($julianCentury, $latitude, $longitude, $depr
$utcTime = 720.0 + $timeDiff - $equationOfTime;
return $utcTime;
}

/**
* Calculate UTC Time of Solar Noon
* @param $julianDay
Expand All @@ -301,7 +302,7 @@ protected function calculateUtcNoonTime($julianDay, $longitude)
$utcTime = 720.0 + (-$longitude * 4.0) - $equationOfTime;
return $utcTime;
}

/**
* Calculate solar time depending on a particular location, date and solar depression
* @param $date
Expand All @@ -327,7 +328,7 @@ protected function calculateSolarTime($date, $latitude, $longitude, $depression,
$utcTime = $this->calculateUtcTime($newJulianCentury, $latitude, $longitude, $depression);
return $this->getUtcDateTime($utcTime, $date);
}

/**
* Get Solar Noon Time, when the Sun is at the highest altitude
* @param $date
Expand All @@ -340,7 +341,7 @@ public function getSolarNoon($date, $longitude, $timeZone = "")
$utcTime = $this->calculateUtcNoonTime($this->calculateJulianDay($date, $timeZone), $longitude);
return $this->getUtcDateTime($utcTime, $date);
}

/**
* Get Sunset hour of a particular location and date
* @param $date
Expand All @@ -353,7 +354,7 @@ public function getSunset($date, $latitude, $longitude, $timeZone = "")
{
return $this->calculateSolarTime($date, $latitude, $longitude, self::SUNSET_DEPRESSION, $timeZone);
}

/**
* Get Sunrise hour of a particular location and date
* @param $date
Expand All @@ -366,7 +367,7 @@ public function getSunrise($date, $latitude, $longitude, $timeZone = "")
{
return $this->calculateSolarTime($date, $latitude, $longitude, self::SUNRISE_DEPRESSION, $timeZone);
}

/**
* Get the beginning of the Civil Twilight phase.
* The Sun is just below the horizon, so there is generally enough natural light to carry out most outdoor
Expand All @@ -382,7 +383,7 @@ public function getCivilTwilightBegin($date, $latitude, $longitude, $timeZone =
{
return $this->calculateSolarTime($date, $latitude, $longitude, self::CIVIL_DEPRESSION, $timeZone);
}

/**
* Get the end of the Civil Twilight phase.
* @param $date
Expand All @@ -395,7 +396,7 @@ public function getCivilTwilightEnd($date, $latitude, $longitude, $timeZone = ""
{
return $this->calculateSolarTime($date, $latitude, $longitude, -self::CIVIL_DEPRESSION, $timeZone);
}

/**
* Get the beginning of the Nautical Twilight phase.
* Nautical twilight is the second twilight phase, when both the horizon and the brighter stars are usually visible
Expand All @@ -411,7 +412,7 @@ public function getNauticalTwilightBegin($date, $latitude, $longitude, $timeZone
{
return $this->calculateSolarTime($date, $latitude, $longitude, self::NAUTICAL_DEPRESSION, $timeZone);
}

/**
* Get the end of the Nautical Twilight phase.
* @param $date
Expand All @@ -424,7 +425,7 @@ public function getNauticalTwilightEnd($date, $latitude, $longitude, $timeZone =
{
return $this->calculateSolarTime($date, $latitude, $longitude, -self::NAUTICAL_DEPRESSION, $timeZone);
}

/**
* Get the beginning of the Astronomical Twilight phase.
* Darkest phase. It is the earliest stage of dawn in the morning and the last stage of dusk in the evening.
Expand All @@ -437,9 +438,14 @@ public function getNauticalTwilightEnd($date, $latitude, $longitude, $timeZone =
*/
public function getAstronomicalTwilightBegin($date, $latitude, $longitude, $timeZone = "")
{
return $this->calculateSolarTime($date, $latitude, $longitude, self::ASTRONOMICAL_DEPRESSION, $timeZone);
return $this->calculateSolarTime(
$date,
$latitude,
$longitude,
self::ASTRONOMICAL_DEPRESSION,
$timeZone);
}

/**
* Get the end of the Astronomical Twilight phase.
* @param $date
Expand Down

0 comments on commit b2cc16c

Please sign in to comment.