Giter Site home page Giter Site logo

porrey / solar-calculator Goto Github PK

View Code? Open in Web Editor NEW
58.0 8.0 16.0 2 MB

Calculates the sunrise and sunset for a given date and location (using GEO coordinates). This library uses the method outlined NOAA Solar Calculations Day spreadsheet found at http://www.esrl.noaa.gov/gmd/grad/solcalc/calcdetails.html.

License: GNU Lesser General Public License v3.0

C# 99.64% Batchfile 0.36%
solar latitude longitude sunset sunrise day polar csharp dotnet night

solar-calculator's Introduction

GitHub Workflow Status (with branch)

Nuget Nuget

Nuget Nuget

Solar-Calculator

Calculates the sunrise and sunset for a given date and location (using GEO coordinates). This library uses the method outlined NOAA Solar Calculations Day spreadsheet found at http://www.esrl.noaa.gov/gmd/grad/solcalc/calcdetails.html.

Installing

Add this to your project in Visual Studio using NuGet Package Manager UI and ID SolarCalculator or using the Package Manager Console.

PM> Install-Package SolarCalculator

Notes

Starting in version 3.3.0 there are two new properties for checking if a date is a Polar Day (IsPolarDay) or Polar Night (IsPolarNight). If the combination of date and location result in a Polar Day or Polar Night, the values for Sunrise and Sunset are as follows:

Polar Day: Sunrise = DateTime.MinValue, Sunset = DateTime.MaxValue

Polar Night: Sunrise = DateTime.MaxValue, Sunset = DateTime.MinValue

Sample Code

The sample code below shows how to initialize and call the Solar Calculator to get the sunrise and sunset for the given location.

Sunrise in Chicago

using System;
using Innovative.SolarCalculator;

//
// Geo coordinates of Oak Street Beach in Chicago, IL
//
// NOTE: the .Date is not necessary but is included to demonstrate that time input 
// does not affect the output. Time will be returned in the current time zone so it 
// will need to be adjusted to the time zone where the coordinates are from (there 
// are services that can be used to get time zone from a latitude and longitude position).
//
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
SolarTimes solarTimes = new SolarTimes(DateTime.Now.Date, 41.9032, -87.6224);
DateTime sunrise = TimeZoneInfo.ConvertTimeFromUtc(solarTimes.Sunrise.ToUniversalTime(), cst);

//
// Display the sunrise
//
Console.WriteLine($"View the sunrise across Lake Michigan from Oak Street Beach in Chicago at {sunrise.ToLongTimeString()} on {sunrise.ToLongDateString()}.");

Sunset in Michigan

//
// Geo coordinates of Benton Harbor/Benton Heights in Michigan
//
TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");              
SolarTimes solarTimes = new SolarTimes(DateTime.Now, 42.1543, -86.4459);
DateTime sunset = TimeZoneInfo.ConvertTimeFromUtc(solarTimes.Sunset.ToUniversalTime(), est);

//
// Display the sunset
//
Console.WriteLine($"View the sunset across Lake Michigan from Benton Harbor in Michigan at {sunset.ToLongTimeString()} on {sunset.ToLongDateString()}.");

solar-calculator's People

Contributors

georgehahn avatar nickztar avatar porrey avatar t1m0thyj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

solar-calculator's Issues

SolarTimes DST regression in 3.0.4

Hello. Version 3.0.4 of the library has a regression calculating sunset times over DST.

For example, the sunset time for 47.606165, -122.342435 on 2017-11-5 is wrong. It should be 16:46:44 local time according to https://sunrise-sunset.org/us/seattle-wa/2017/11 but SolarTimes returns 17:46:44, which would be correct if it weren't for DST.

Please let me know if you have trouble reproducing this. It works in 3.0.3.

Thanks.

SolarTimes: SolarNoon should have the same date as the requested date

Instead, it's set to the current date.

This can be worked around, but ideally it would be the same date as sunrise, sunset, aka the requested date.

Here's what I'm seeing in VS with 2.0.1:

-		solar	{Innovative.SolarCalculator.SolarTimes}	Innovative.SolarCalculator.SolarTimes
+		AtmosphericRefraction	{0°.8330}	Innovative.Geometry.Angle
		EccentricityOfEarthOrbit	0.0167011631360033348253412462	decimal
		EquationOfTime	11.874755288182159680353068853	decimal
+		ForDate	{10/6/2017 12:00:00 AM -07:00}	System.DateTimeOffset
+		HourAngleSunrise	{87°.08060873}	Innovative.Geometry.Angle
		JulianCentury	0.1776260552133242071640428939	decimal
		JulianDay	2458032.7916666666666666666667	decimal
+		Latitude	{37°.17404886}	Innovative.Geometry.Angle
+		Longitude	{-113°.01283544}	Innovative.Geometry.Angle
+		MeanEclipticObliquity	{23°.43698123}	Innovative.Geometry.Angle
+		ObliquityCorrection	{23°.43497811}	Innovative.Geometry.Angle
+		SolarDeclination	{-5°.21791966}	Innovative.Geometry.Angle
+		SolarNoon	{10/1/2017 12:20:10 PM}	System.DateTime
+		SunApparentLongitude	{193°.21880195}	Innovative.Geometry.Angle
+		SunEquationOfCenter	{-1°.9137307}	Innovative.Geometry.Angle
+		SunGeometricMeanLongitude	{195°.14119911}	Innovative.Geometry.Angle
+		SunMeanAnomaly	{6751°.89839959}	Innovative.Geometry.Angle
+		SunTrueLongitude	{193°.22746842}	Innovative.Geometry.Angle
+		SunlightDuration	{11:36:38.6920000}	System.TimeSpan
+		Sunrise	{10/6/2017 6:31:51 AM}	System.DateTime
+		Sunset	{10/6/2017 6:08:29 PM}	System.DateTime
		TimePastLocalMidnight	0	decimal
		TimeZoneOffset	-7	decimal
		TrueSolarTime	1419.8234135131501596803530688	decimal
		VarY	0.0430182430890316477981212096	decimal

Why the Microsoft Reciprocal License?

It is incompatible with the GNU GPL and also requires all files that contains code from the program and all modified or extended versions of the program to be open sourced as well...

Could you use a less restrictive and more compatible license?

How to calculate vector for the sun point downwards?

I just come across your library and it looks great,
I have one question How to calculate the vector for the sun point downwards?
for given :

Latitude (+ to N), Longitude (+ to E), Time Zone (+ to E), Local Time (hrs) and Year

Do you have any documentation or some time for a meeting?

image

image

Add properties for Dawn and Dusk

G'Day,

I like your library but think it could be better if some more properties were calculated.
Specifically I'm thinking of:

  • Astronomical Dawn
  • Nautical Dawn
  • Civil Dawn
  • Civil Dusk
  • Nautical Dusk
  • Astronomical Dusk

I'm happy to help our am looking through your code.
But if you know how to fix it before I can create a pull request that would make my life easier.

Can't use version 2 in Xamarin.Forms

I was successfully using version 1 of Solar Calculator in my Xamarin.Forms project, except that this package caused problems when I wanted to internationalise the app (so I had to delay i18n). Now I noticed that version 2 is available and hoped that i18n issue might be fixed in it. Unfortunately, my attempt to update the package failed with the following message:

Could not install package 'SolarCalculator 2.0.4'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile78', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Is it possible to know what should I change to get this package installed?

Thanks!

On day of time changes, the sunrise is off by 1 if the time is set to midnight

I noticed an issue that might be a bug, or at least something that I misunderstood from the examples provided in the README.

Example: We have a location in Berlin, Germany. We now want to calculate the sunrise for the following three days

  • day before winter time change: Oct 30 2021
  • day of winter time change: Oct 31 2021
  • day after winter time change: Nov 1 2021

The way I used the library, the correct sunrise/sunset was calculated for the day before and the day after, but not for the day of the winter time change.

  • Oct 30 2021: 2021-10-30T07:58:41
  • Oct 31 2021: 2021-10-31T08:00:35 (wrong, off by one hour, should be 07:00:35)
  • Nov 01 2021: 2021-11-01T07:02:25

For each case I created a DateTime with Kind Unspecified. So for the example of Oct 31:

var date = new DateTime(2021, 10, 31, 0, 0, 0);
var solarTimes = new SolarTimes(date, 52.50, 13.35);

This calculates the wrong date, because at 00:00 the UTC offset is still 2. At 3:00 the UTC offset changes to 1. The offset is used in internal calculations, which I found out after debugging a while.

So as a workaround/fix, we are now always passing a DateTime set to noon (12:00).

var date = new DateTime(2021, 10, 31, 12, 0, 0);
var solarTimes = new SolarTimes(date, 52.50, 13.35);

Then it works very well.

In my mind that seems like a bug, because even if you are checking the sunrise/sunset of a day before 3:00 AM (e.g. via DateTime.Now), you should be able to get the correct sunrise/sunset for that day.

The above examples are a bit simplified from my real code and I didn't double check them, but the point is that some days have 2 valid UTC offsets, and if the wrong one is accidentally used, then the sunrise is off by 1.

Did I misunderstand something maybe, or might this be a bug?

OverflowException for latitudes in polar circle especially during polar day/night

The calculation overflows for locations that are closer to the poles (~ ±66° latitude) especially for dates during polar day or night.

Unhandled Exception: System.OverflowException: Value was either too large or too small for a Decimal.
   at System.Decimal..ctor(Double value)
   at Innovative.SolarCalculator.Universal.Math.Acos(Decimal value)
   at Innovative.SolarCalculator.SolarTimes.get_HourAngleSunrise()
   at Innovative.SolarCalculator.SolarTimes.get_Sunrise()

Example:

// Tromsø, Norway during polar night
new SolarTimes(new DateTime(2020, 1, 1), new Angle(69.6529), new Angle(18.9641)).Sunrise

// south 2 degrees latitude during polar night does NOT overflow
new SolarTimes(new DateTime(2020, 1, 1), new Angle(67.6529), new Angle(18.9641)).Sunrise

// south 2 degrees latitude during polar day DOES overflow
new SolarTimes(new DateTime(2020, 6, 1), new Angle(67.6529), new Angle(18.9641)).Sunrise

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.