++ed by:
SREZIC

1 PAUSE user

Jean Forget
and 1 contributors

NAME

Astro::Sunrise - Perl extension for computing the sunrise/sunset on a given day

VERSION

This documentation refers to Astro::Sunrise version 0.95.

SYNOPSIS

  # When will the sun rise on YAPC::Europe 2015?
  use Astro::Sunrise;
  my ($sunrise, $sunset) = sunrise( { year => 2015, month => 9, day => 2, # YAPC::EU starts on 2nd September 2015
                                      lon  => -3.6, lat   => 37.17,       # Granada is 37°10'N, 3°36'W
                                      tz   => 1,    dst   => 1 } );       # This is still summer, therefore DST

  # When does the sun rise today in Salt Lake City (home to YAPC::NA 2015)?
  use Astro::Sunrise;
  use DateTime;
  $sunrise_today = sun_rise( { lon => -111.88, lat => 40.75 } ); # 40°45'N, 111°53'W

  # And when does it set tomorrow at Salt Lake City?
  use Astro::Sunrise;
  use DateTime;
  $sunset_tomorrow = sun_set( { lat => 40.75,    # 40°45'N,
                                lon => -111.88,  # 111°53'W
                                alt => -0.833,   # standard value for the sun altitude at sunset
                                offset => 1 } ); # day offset up to tomorrow

DESCRIPTION

This module will return the sunrise and sunset for a given day.

Months are numbered 1 to 12, in the usual way, not 0 to 11 as in C and in Perl's localtime.

 Eastern longitude is entered as a positive number
 Western longitude is entered as a negative number
 Northern latitude is entered as a positive number
 Southern latitude is entered as a negative number

Please note that, when given as positional parameters, the longitude is specified before the latitude.

The time zone is given as the numeric value of the offset from UTC.

The precise parameter is set to either 0 or 1. If set to 0 no Iteration will occur. If set to 1 Iteration will occur, which will give a more precise result. Default is 0.

There are a number of sun altitudes to chose from. The default is -0.833 because this is what most countries use. Feel free to specify it if you need to. Here is the list of values to specify altitude (ALT) with, including symbolic constants for each.

0 degrees

Center of Sun's disk touches a mathematical horizon

-0.25 degrees

Sun's upper limb touches a mathematical horizon

-0.583 degrees

Center of Sun's disk touches the horizon; atmospheric refraction accounted for

-0.833 degrees, DEFAULT

Sun's upper limb touches the horizon; atmospheric refraction accounted for

-6 degrees, CIVIL

Civil twilight (one can no longer read outside without artificial illumination)

-12 degrees, NAUTICAL

Nautical twilight (navigation using a sea horizon no longer possible)

-15 degrees, AMATEUR

Amateur astronomical twilight (the sky is dark enough for most astronomical observations)

-18 degrees, ASTRONOMICAL

Astronomical twilight (the sky is completely dark)

USAGE

sunrise

  ($sunrise, $sunset) = sunrise( { year    => $year,      month      => $month,     day => $day,
                                   lon     => $longitude, lat        => $latitude,
                                   tz      => $tz_offset, isdst      => $is_dst,
                                   alt     => $altitude,  upper_limb => $upper_limb,
                                   precise => $precise,   polar      => $action } );

  ($sunrise, $sunset) = sunrise(YYYY,MM,DD,longitude,latitude,Time Zone,DST);

  ($sunrise, $sunset) = sunrise(YYYY,MM,DD,longitude,latitude,Time Zone,DST,ALT);

  ($sunrise, $sunset) = sunrise(YYYY,MM,DD,longitude,latitude,Time Zone,DST,ALT,precise);

Returns the sunrise and sunset times, in HH:MM format.

The first form uses a hash reference to pass arguments by name. The other forms are kept for backward compatibility. The arguments are:

year, month, day

The three elements of the date for which you want to compute the sunrise and sunset. Months are numbered 1 to 12, in the usual way, not 0 to 11 as in C and in Perl's localtime.

Mandatory, can be positional (#1, #2 and #3).

lon, lat

The longitude and latitude of the place for which you want to compute the sunrise and sunset. They are given in decimal degrees. For example:

    lon => -3.6,  #  3° 36' W
    lat => 37.17, # 37° 10' N

 Eastern longitude is entered as a positive number
 Western longitude is entered as a negative number
 Northern latitude is entered as a positive number
 Southern latitude is entered as a negative number

Mandatory, can be positional (#4 and #5).

tz

Time Zone is the offset from GMT

Mandatory, can be positional (#6).

isdst

1 if daylight saving time is in effect, 0 if not.

Mandatory, can be positional (#7).

alt

Altitude of the sun, in decimal degrees. Usually a negative number, because the sun should be under the mathematical horizon. But if there is a high mountain range sunward (that is, southward if you live in the Northern hemisphere), you may need to enter a positive altitude.

This parameter is optional. Its default value is -0.833. It can be positional (#8).

upper_limb

If this parameter set to a true value (usually 1), the algorithm computes the sun apparent radius and takes it into account when computing the sun altitude. This parameter is useful only when the alt parameter is set to 0 or -0.583 degrees. When using -0.25 or -0.833 degrees, the sun radius is already taken into account. When computing twilights (-6 to -18), the sun radius is irrelevant.

Since the default value for the alt parameter is -0.833, the default value for upper_limb is 0.

This parameter is optional and it can be specified only by keyword.

polar

When dealing with a polar location, there may be dates where there is a polar night (sun never rises) or a polar day. The default behaviour of the module is to emit a warning in these cases ("Sun never rises!!" or "Sun never sets!!"). But some programmers may find this inconvenient. An alternate behaviour is to return special values reflecting the situation.

So, if the polar parameter is set to 'warn', the module emits a warning. If the polar parameter is set to 'retval', the module emits no warning, but it returns either 'day' or 'night'.

Example:

  # Loosely based on Alex Gough's activities: scientist and Perl programmer, who spent a year
  # in Halley Base in 2006. Let us suppose he arrived there on 15th January 2006.
  my ($sunrise, $sunset) = sunrise( { year => 2006, month => 1, day => 15,
                                      lon => -26.65, lat => -75.58, # Halley Base: 75°35'S 26°39'W
                                      tz  => 3, polar => 'retval' } );
  if ($sunrise eq 'day') {
    say "Alex Gough saw the midnight sun the first day he arrived at Halley Base";
  }
  elsif ($sunrise eq 'night') {
    say "It would be days, maybe weeks, before the sun would rise.";
  }
  else {
    say "Alex saw his first antarctic sunset at $sunset";
  }

This parameter is optional and it can be specified only by keyword.

precise

Choice between a precise algorithm and a simpler algorithm. The default value is 0, that is, the simpler algorithm. Any true value switches to the precise algorithm.

The original method only gives an approximate value of the Sun's rise/set times. The error rarely exceeds one or two minutes, but at high latitudes, when the Midnight Sun soon will start or just has ended, the errors may be much larger. If you want higher accuracy, you must then use the precise algorithm. This feature is new as of version 0.7. Here is what I have tried to accomplish with this.

a) Compute sunrise or sunset as always, with one exception: to convert LHA from degrees to hours, divide by 15.04107 instead of 15.0 (this accounts for the difference between the solar day and the sidereal day).

b) Re-do the computation but compute the Sun's RA and Decl, and also GMST0, for the moment of sunrise or sunset last computed.

c) Iterate b) until the computed sunrise or sunset no longer changes significantly. Usually 2 iterations are enough, in rare cases 3 or 4 iterations may be needed.

This parameter is optional. It can be positional (#9).

For Example

 ($sunrise, $sunset) = sunrise( 2001, 3, 10, 17.384, 98.625, -5, 0 );
 ($sunrise, $sunset) = sunrise( 2002, 10, 14, -105.181, 41.324, -7, 1, -18);
 ($sunrise, $sunset) = sunrise( 2002, 10, 14, -105.181, 41.324, -7, 1, -18, 1);

sun_rise, sun_set

  $sun_rise = sun_rise( { lon => $longitude, lat => $latitude,
                          alt => $altitude, upper_limb => $bool,
                          offset  => $day_offset,
                          precise => $bool_precise, polar => $action } );
  $sun_set  = sun_set ( { lon => $longitude, lat => $latitude,
                          alt => $altitude, upper_limb => $bool,
                          offset  => $day_offset,
                          precise => $bool_precise, polar => $action } );
  $sun_rise = sun_rise( $longitude, $latitude );
  $sun_rise = sun_rise( $longitude, $latitude, $alt );
  $sun_rise = sun_rise( $longitude, $latitude, $alt, $day_offset );

Returns the sun rise time (resp. the sun set time) for the given location and for today's date (as given by DateTime), plus or minus some offset in days. The first form use all parameters and transmit them by name. The second form uses today's date (from DateTime) and the default altitude. The third form adds specifying a custom altitude. The fourth form allows for specifying an integer day offset from today, either positive or negative.

The parameters are the same as the parameters for sunrise. There is an additional parameter, offset, which allows using a date other than today: +1 for to-morrow, -7 for one week ago, etc.

The arguments are:

lon, lat

The longitude and latitude of the place for which you want to compute the sunrise and sunset. They are given in decimal degrees. For example:

    lon => -3.6,  #  3° 36' W
    lat => 37.17, # 37° 10' N

 Eastern longitude is entered as a positive number
 Western longitude is entered as a negative number
 Northern latitude is entered as a positive number
 Southern latitude is entered as a negative number

Mandatory, can be positional (#1 and #2).

alt

Altitude of the sun, in decimal degrees. Usually a negative number, because the sun should be under the mathematical horizon. But if there is a high mountain range sunward (that is, southward if you live in the Northern hemisphere), you may need to enter a positive altitude.

This parameter is optional. Its default value is -0.833. It can be positional (#3).

offset

By default, sun_rise and sun_set use the current day. If you need another day, you give an offset relative to the current day. For example, +7 means next week, while -365 means last year.

This parameter has nothing to do with timezones.

Optional, 0 by default, can be positional (#4).

time_zone

Time Zone is the Olson name for a timezone. By default, the functions sun_rise and sun_set will try to use the local timezone.

This parameter is optional and it can be specified only by keyword.

upper_limb

If this parameter set to a true value (usually 1), the algorithm computes the sun apparent radius and takes it into account when computing the sun altitude. This parameter is useful only when the alt parameter is set to 0 or -0.583 degrees. When using -0.25 or -0.833 degrees, the sun radius is already taken into account. When computing twilights (-6 to -18), the sun radius is irrelevant.

Since the default value for the alt parameter is -0.833, the default value for upper_limb is 0.

This parameter is optional and it can be specified only by keyword.

polar

When dealing with a polar location, there may be dates where there is a polar night (sun never rises) or a polar day. The default behaviour of the module is to emit a warning in these cases ("Sun never rises!!" or "Sun never sets!!"). But some programmers may find this inconvenient. An alternate behaviour is to return special values reflecting the situation.

So, if the polar parameter is set to 'warn', the module emits a warning. If the polar parameter is set to 'retval', the module emits no warning, but it returns either 'day' or 'night'.

This parameter is optional and it can be specified only by keyword.

precise

Choice between a precise algorithm and a simpler algorithm. The default value is 0, that is, the simpler algorithm. Any true value switches to the precise algorithm.

For more documentation, see the corresponding parameter for the sunrise function.

This parameter is optional and it can be specified only by keyword.

For Example

 $sunrise = sun_rise( -105.181, 41.324 );
 $sunrise = sun_rise( -105.181, 41.324, -15 );
 $sunrise = sun_rise( -105.181, 41.324, -12, +3 );
 $sunrise = sun_rise( -105.181, 41.324, undef, -12);

Trigonometric functions using degrees

Since the module use trigonometry with degrees, the corresponding functions are available to the module user, free of charge. Just mention the tag :trig in the use statement. These functions are:

sind, cosd, tand

The direct functions, that is, sine, cosine and tangent functions, respectively. Each one receives one parameter, in degrees, and returns the trigonometric value.

asind, acosd, atand

The reverse functions, that is, arc-sine, arc-cosine, and arc-tangent. Each one receives one parameter, the trigonometric value, and returns the corresponding angle in degrees.

atan2d

Arc-tangent. This function receives two parameters: the numerator and the denominator of a fraction equal to the tangent. Use this function instead of atand when you are not sure the denominator is not zero. E.g.:

  use Astro::Sunrise qw(:trig);
  say atan2d(1, 2) # prints 26,5
  say atan2d(1, 0) # prints 90, without triggering a "division by zero" error
equal

Not really a trigonometrical function, but still useful at some times. This function receives two floating values and an integer value. It compares the floating numbers, and returns "1" if their most significant digits are equal. The integer value specifies how many digits are kept. E.g.

  say equal(22/7, 355/113, 3) # prints 1, because :  22/7   = 3.14285715286 rounded to 3.14
                              #                     355/113 = 3.14159292035 rounded to 3.14
  say equal(22/7, 355/113, 4) # prints 0, because :  22/7   = 3.14285715286 rounded to 3.143
                              #                     355/113 = 3.14159292035 rounded to 3.142

EXPORTS

By default, the functions sunrise, sun_rise and sun_set are exported.

The constants DEFAULT, CIVIL, NAUTICAL, AMATEUR and ASTRONOMICAL are exported on request with the tag :constants.

The functions sind, cosd, tand, asind, acosd, atand, atan2d and equal exported on request with the tag :trig.

DEPENDENCIES

This module requires only core modules: POSIX, Math::Trig and Carp.

If you use the sun_rise and sun_set functions, you will need also DateTime.

AUTHOR

Ron Hill rkhill@firstlight.net

Co-maintainer: Jean Forget (JFORGET at cpan dot org)

SPECIAL THANKS

Robert Creager [Astro-Sunrise@LogicalChaos.org] for providing help with converting Paul's C code to Perl, for providing code for sun_rise, sun_set subs. Also adding options for different altitudes.

Joshua Hoblitt [jhoblitt@ifa.hawaii.edu] for providing the patch to convert to DateTime.

Chris Phillips for providing patch for conversion to local time.

Brian D Foy for providing patch for constants :)

Gabor Szabo for pointing POD mistakes.

CREDITS

Paul Schlyter, Stockholm, Sweden

for his excellent web page on the subject.

Rich Bowen (rbowen@rbowen.com)

for suggestions.

Adrian Blockley [adrian.blockley@environ.wa.gov.au]

for finding a bug in the conversion to local time.

Slaven Rezić

for finding and fixing a bug with DST.

Lightly verified against http://aa.usno.navy.mil/data/docs/RS_OneYear.html

In addition, checked to be compatible with a C implementation of Paul Schlyter's algorithm.

COPYRIGHT and LICENSE

Perl Module

This program is distributed under the same terms as Perl 5.16.3: GNU Public License version 1 or later and Perl Artistic License

You can find the text of the licenses in the LICENSE file or at http://www.perlfoundation.org/artistic_license_1_0 and http://www.gnu.org/licenses/gpl-1.0.html.

Here is the summary of GPL:

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., http://www.fsf.org/.

Original C program

Here is the copyright information provided by Paul Schlyter:

Written as DAYLEN.C, 1989-08-16

Modified to SUNRISET.C, 1992-12-01

(c) Paul Schlyter, 1989, 1992

Released to the public domain by Paul Schlyter, December 1992

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

BUGS

SEE ALSO

perl(1).

DateTime::Event::Sunrise

DateTime::Event::Jewish::Sunrise