Source code: C++ Sun Tracking and Calculation Classes šŸ› 

A good portion of the wrangling of the algorithms is done on this piece. Build a port, it would be lovely and quite useful.

I know that I would be interested in utilizing something like that at some pointā€¦

1 Like

Example snippet that determines and plots the daily distance to the sun throughout the year for New York using the class in the op.
This is one of the preliminary steps, for instance, that can be used for calculating the extraterrestrial solar radiation striking the earthā€™s atmosphere (and for eventually estimating the DLI at your plants).

        /********************/
        /* Calculate and plot the distance over the course of a year for New York, find the minimum and maximum distance. */
        zoned_sun_time newyork_area = date::make_zoned("America/New_York", date::local_time<date::days>{ date::January / 01 / 2018 });
        std::cout << newyork_area << '\n' << std::endl;

		sun sun_element_newyork;
		double distmax, distmax_copy, distmin_copy, distmin = 1.0;
		for (int day = 1; day < 365; day++)
		{
			/* Find the day with the maximum and minimum amounts of daylight. */
			sun_element_newyork.set("New York", 40.730610, -73.935242, newyork_area);
			double distance = sun_element_newyork.rad_vector;
        
			if (distance > distmax) distmax = distance;
			if (distance < distmin) distmin = distance;
        
			newyork_area = newyork_area.get_local_time() + date::days{ 1 };
		}
    
		distmax_copy = distmax;
		distmax *= (AU_KM);
		int distmax_ceil = ceil(distmax);
		distmin_copy = distmin;
		distmin *= (AU_KM);
		int distmin_floor = floor(distmin);	
		double chart_x_quant = 96.0 / (distmax_ceil - distmin_floor);

		/* Plot the chart */
		int day_stride = 4;
		newyork_area = date::make_zoned("America/New_York", date::local_days{ date::January / 01 / 2018 });
		for (int day = 1; day < 365; day += 1)
		{
			sun_element_newyork.set("New York", 40.730610, -73.935242, newyork_area);
			double distance = sun_element_newyork.rad_vector;
        
			if ((distance) >= (distmax_copy) || (distance) <= (distmin_copy) || (day % day_stride == 0))
			{

				std::cout << date::format("%b %d %Y", newyork_area) << ": " << std::string(round(((distance * (AU_KM)) - distmin)*chart_x_quant), '*');
        
				if ((distance) >= (distmax_copy))
				{
					std::cout << " <-max- " << distance * (AU_KM) << " kilometers";
				}
            
				if ((distance) <= (distmin_copy))
				{
					std::cout << " <-min- " << distance * (AU_KM) << " kilometers";
				}
        
				std::cout << std::endl;
			}	    
        
			newyork_area = newyork_area.get_local_time() + date::days{ 1 };
		}
		std::cout << std::endl;
	
	} while (0);
Jan 03 2018:  <-min- 1.471e+08 kilometers
Jan 04 2018:
Jan 08 2018:
Jan 12 2018: *
Jan 16 2018: *
Jan 20 2018: **
Jan 24 2018: ***
Jan 28 2018: ****
Feb 01 2018: ******
Feb 05 2018: ********
Feb 09 2018: *********
Feb 13 2018: ************
Feb 17 2018: **************
Feb 21 2018: ****************
Feb 25 2018: *******************
Mar 01 2018: **********************
Mar 05 2018: ************************
Mar 09 2018: ***************************
Mar 13 2018: ******************************
Mar 17 2018: *********************************
Mar 21 2018: *************************************
Mar 25 2018: ****************************************
Mar 29 2018: *******************************************
Apr 02 2018: **********************************************
Apr 06 2018: **************************************************
Apr 10 2018: *****************************************************
Apr 14 2018: ********************************************************
Apr 18 2018: ************************************************************
Apr 22 2018: ***************************************************************
Apr 26 2018: ******************************************************************
Apr 30 2018: *********************************************************************
May 04 2018: ************************************************************************
May 08 2018: **************************************************************************
May 12 2018: *****************************************************************************
May 16 2018: ********************************************************************************
May 20 2018: **********************************************************************************
May 24 2018: ************************************************************************************
May 28 2018: **************************************************************************************
Jun 01 2018: ****************************************************************************************
Jun 05 2018: ******************************************************************************************
Jun 09 2018: *******************************************************************************************
Jun 13 2018: *********************************************************************************************
Jun 17 2018: **********************************************************************************************
Jun 21 2018: ***********************************************************************************************
Jun 25 2018: ***********************************************************************************************
Jun 29 2018: ************************************************************************************************
Jul 03 2018: ************************************************************************************************
Jul 05 2018: ************************************************************************************************ <-max- 1.52096e+08 kilometers
Jul 07 2018: ************************************************************************************************
Jul 11 2018: ************************************************************************************************
Jul 15 2018: ***********************************************************************************************
Jul 19 2018: ***********************************************************************************************
Jul 23 2018: **********************************************************************************************
Jul 27 2018: *********************************************************************************************
Jul 31 2018: *******************************************************************************************
Aug 04 2018: ******************************************************************************************
Aug 08 2018: ****************************************************************************************
Aug 12 2018: **************************************************************************************
Aug 16 2018: ************************************************************************************
Aug 20 2018: **********************************************************************************
Aug 24 2018: ********************************************************************************
Aug 28 2018: *****************************************************************************
Sep 01 2018: ***************************************************************************
Sep 05 2018: ************************************************************************
Sep 09 2018: *********************************************************************
Sep 13 2018: ******************************************************************
Sep 17 2018: ***************************************************************
Sep 21 2018: ************************************************************
Sep 25 2018: *********************************************************
Sep 29 2018: *****************************************************
Oct 03 2018: **************************************************
Oct 07 2018: ***********************************************
Oct 11 2018: *******************************************
Oct 15 2018: ****************************************
Oct 19 2018: *************************************
Oct 23 2018: **********************************
Oct 27 2018: *******************************
Oct 31 2018: ***************************
Nov 04 2018: *************************
Nov 08 2018: **********************
Nov 12 2018: *******************
Nov 16 2018: ****************
Nov 20 2018: **************
Nov 24 2018: ************
Nov 28 2018: **********
Dec 02 2018: ********
Dec 06 2018: ******
Dec 10 2018: ****
Dec 14 2018: ***
Dec 18 2018: **
Dec 22 2018: *
Dec 26 2018: *
Dec 30 2018:

CC BY-SA 4.0

1 Like

dude, im so happy i found this forumā€¦ as you know im building code for smart grow box controller, or should i say i started, ive done enough research so i can start with something, i really hope ill make this come true, but anyway, its hard to make language agnostic code, micro controllers that are used to build smart things (sense and react) either use python or c++ or arduino finetuned c+ (which im using, cause them libraries)ā€¦ thankfully, by effort of many ppl like yourself, there are a lot of great open source libraries and made code, that there is only need to copy/paste it, finetune it, and test itā€¦ you dont need to decode i2c signal from a sensor, you just type in a function and boom, youre talking to a sensorā€¦ i appreciate the spirit, i personally fell in love with open source idea, and cant wait to contribute myself to it (wont be as detailed as your work, as i said, basically find all the libraries and fine tune them to work together)ā€¦ peace and <3

2 Likes

Oh, yes. Please do. Would like to have as many folk utilize, contribute, and share as possible.

As youā€™ve noted, everyone has a different way of doing things including different languages theyā€™d rather be using. But, in many cases, the core algorithms can easily be ported.

Some of what is posted here is meant as a base for other endeavors including, as youā€™ve noted, integrating with sensors and such. Or, creation of entirely new classes of instruments, new ideas, different ways to go about thingsā€¦

1 Like

Very helpful. Thank you for sharing the code.
Is it possible to share the tz.h file or advice on where to find it?

Cheers,

1 Like

Hi @Dheyaa,

My apologizes, the tz.h comes from a third part utility library called ā€˜dateā€™, it is available on GitHub here:

It is meant to allow conversions and mathematical operations on times,dates, and timezones.

1 Like

Thanks a lot for the help. very appreciated.

1 Like

Is there anybody around here who run that code in windows. I want to use it in Visual c++ form application. But I couldnā€™t compile date library. C++ CLI doesnā€™t support C++20.

Is it just the date library from HowardHinnant that is causing the grief? There are several different release versions. The master branch is at 3.0+. An older release version might work better in your scenario.