using Autodesk.DesignScript.Geometry; using Autodesk.DesignScript.Runtime; using MoreLinq; using System; using System.Collections.Generic; using System.Linq; namespace Solar { public static class Positioning { [MultiReturn(new[] { "Vector", "Elevation", "Azimuth", "Zenith", "Incidence", "Sunrise", "Sunset", "Transit" })] public static Dictionary ByYearMonthDay(Double Latitude, Double Longitude, Double Elevation, int Year, int Month, int Day, int Hour, Double Timezone, int Minute = 0, int Second = 0, Double Pressure = 1000, Double Temperature = 18, Double Slope = 0) { Dictionary d = new Dictionary(); try { SPA.Data spd = new SPA.Data(); spd.Year = Year; spd.Month = Month; spd.Day = Day; spd.Hour = Hour; spd.Minute = Minute; spd.Second = Second; spd.Timezone = Timezone; spd.DeltaUt1 = 0; spd.DeltaT = 67; spd.Latitude = Latitude; spd.Longitude = Longitude; spd.Elevation = Elevation; spd.Pressure = Pressure; spd.Temperature = Temperature; spd.Slope = Slope; spd.AzmRotation = 0; spd.AtmosRefract = 0.5667; spd.Function = SPA.CalculationMode.SPA_ZA_RTS; var result = SPA.SPACalculate(ref spd); if (result == 0) { Vector v = Vector.ByCoordinates(Math.Sin(spd.Azimuth * (Math.PI / 180)), Math.Cos(spd.Azimuth * (Math.PI / 180)), spd.E * (Math.PI / 180)).Normalized(); d.Add("Vector", v); d.Add("Elevation", 90 - spd.Zenith); d.Add("Azimuth", spd.Azimuth); d.Add("Zenith", spd.Zenith); d.Add("Incidence", spd.Incidence); d.Add("Sunrise", TimeSpan.FromHours(Math.Round(spd.Sunrise, 4))); d.Add("Sunset", TimeSpan.FromHours(Math.Round(spd.Sunset, 5))); d.Add("Transit", TimeSpan.FromHours(Math.Round(spd.Suntransit, 6))); } else { return null; } } catch (Exception ae) { String strError = ae.ToString(); } return d; } [MultiReturn(new[] { "Vector", "Elevation", "Azimuth", "Zenith", "Incidence", "Sunrise", "Sunset", "Transit" })] public static Dictionary ByDateTime(Double Latitude, Double Longitude, Double Elevation, DateTime dateTime, Double Timezone, Double Pressure = 1000, Double Temperature = 18, Double Slope = 0) { Dictionary d = new Dictionary(); try { SPA.Data spd = new SPA.Data(); spd.Year = dateTime.Year; spd.Month = dateTime.Month; spd.Day = dateTime.Day; spd.Hour = dateTime.Hour; spd.Minute = dateTime.Minute; spd.Second = dateTime.Second; spd.Timezone = Timezone; spd.DeltaUt1 = 0; spd.DeltaT = 67; spd.Latitude = Latitude; spd.Longitude = Longitude; spd.Elevation = Elevation; spd.Pressure = Pressure; spd.Temperature = Temperature; spd.Slope = Slope; spd.AzmRotation = 0; spd.AtmosRefract = 0.5667; spd.Function = SPA.CalculationMode.SPA_ZA_RTS; var result = SPA.SPACalculate(ref spd); if (result == 0) { Vector v = Vector.ByCoordinates(Math.Sin(spd.Azimuth * (Math.PI / 180)), Math.Cos(spd.Azimuth * (Math.PI / 180)), spd.E * (Math.PI / 180)).Normalized(); d.Add("Vector", v); d.Add("Elevation", 90 - spd.Zenith); d.Add("Azimuth", spd.Azimuth); d.Add("Zenith", spd.Zenith); d.Add("Incidence", spd.Incidence); d.Add("Sunrise", TimeSpan.FromHours(Math.Round(spd.Sunrise, 4))); d.Add("Sunset", TimeSpan.FromHours(Math.Round(spd.Sunset, 5))); d.Add("Transit", TimeSpan.FromHours(Math.Round(spd.Suntransit, 6))); } else { return null; } } catch (Exception ae) { String strError = ae.ToString(); } return d; } //[MultiReturn(new[] { "Vector", "Elevation", "Azimuth", "Zenith", "Incidence", "Sunrise", "Sunset", "Transit" })] //public static Dictionary> ByNestedYearMonthDay( // List Latitudes, List Longitudes, List Elevations, // List Year, List Month, List Day, // List Hour, List Minute, List Second, // List Timezones, List Pressures, // List Temperatures, List Slopes) //{ // //Check if latitude, longitude // if (Latitudes.Count == Longitudes.Count) // { return null; } // int iLatLon = LatLonEle(Latitudes, Longitudes, Elevations); // Dictionary ListLens = ListLengths( // Latitudes, Longitudes, Elevations, // Year, Month, Day, Hour, Minute, Second, // Timezones, Pressures, Temperatures, Slopes); // KeyValuePair m = ListLens.MaxBy(e => e.Value).First(); // long NrTimeValues = Year.Distinct().Count() * Month.Distinct().Count() * // Day.Distinct().Count() * Hour.Distinct().Count() * Minute.Distinct().Count() * // Second.Distinct().Count(); // //Multiple all list by relevant mulitple to achieve identical list/dictionary lengths // //Check other variables to determine whether increasing the list size is required // List lMultiplesReq = new List(); // Dictionary> ld = new Dictionary>(); // try // { // for (int i = 0; i < Latitudes.Count; i++) // { // Dictionary d = new Dictionary(); // SPA.Data spd = new SPA.Data(); // spd.Year = Year[i]; // spd.Month = Month[i]; // spd.Day = Day[i]; // spd.Hour = Hour[i]; // spd.Minute = Minute[i]; // spd.Second = Second[i]; // spd.Timezone = Timezones[i]; // spd.DeltaUt1 = 0; // spd.DeltaT = 67; // spd.Latitude = Latitudes[i]; // spd.Longitude = Longitudes[i]; // spd.Elevation = Elevations[i]; // spd.Pressure = Pressures[i]; // spd.Temperature = Temperatures[i]; // spd.Slope = Slopes[i]; // spd.AzmRotation = 0; // spd.AtmosRefract = 0.5667; // spd.Function = SPA.CalculationMode.SPA_ZA_RTS; // var result = SPA.SPACalculate(ref spd); // if (result == 0) // { // Vector v = Vector.ByCoordinates(Math.Sin(spd.Azimuth * (Math.PI / 180)), // Math.Cos(spd.Azimuth * (Math.PI / 180)), // spd.E * (Math.PI / 180)).Normalized(); // d.Add("Vector", v); // d.Add("Elevation", 90 - spd.Zenith); // d.Add("Azimuth", spd.Azimuth); // d.Add("Zenith", spd.Zenith); // d.Add("Incidence", spd.Incidence); // d.Add("Sunrise", TimeSpan.FromHours(Math.Round(spd.Sunrise, 4))); // d.Add("Sunset", TimeSpan.FromHours(Math.Round(spd.Sunset, 5))); // d.Add("Transit", TimeSpan.FromHours(Math.Round(spd.Suntransit, 6))); // } // else // { return null; } // ld.Add(i, d); // } // } // catch (Exception ae) // { // String strError = ae.ToString(); // } // return ld; //} //private static Dictionary ListLengths( // List Latitudes, List Longitudes, List Elevations, // List Year, List Month, List Day, // List Hour, List Minute, List Second, // List Timezones, List Pressures, // List Temperatures, List Slopes) //{ // //Determine list lengths // Dictionary lListLengths = new Dictionary(); // lListLengths.Add(Latitudes.ToString(), Latitudes.Count); // lListLengths.Add(Longitudes.ToString(), Longitudes.Count); // lListLengths.Add(Elevations.ToString(), Elevations.Count); // lListLengths.Add(Year.ToString(), Year.Count); // lListLengths.Add(Month.ToString(), Month.Count); // lListLengths.Add(Day.ToString(), Day.Count); // lListLengths.Add(Hour.ToString(), Hour.Count); // lListLengths.Add(Minute.ToString(), Minute.Count); // lListLengths.Add(Second.ToString(), Second.Count); // lListLengths.Add(Timezones.ToString(), Timezones.Count); // lListLengths.Add(Pressures.ToString(), Pressures.Count); // lListLengths.Add(Temperatures.ToString(), Temperatures.Count); // lListLengths.Add(Slopes.ToString(), Slopes.Count); // return lListLengths; //} //private static int LatLonEle(List Latitudes, // List Longitudes, List Elevations) //{ // int Out = 0; // if (Elevations.Count != Latitudes.Count) // { // Out = LowecstCommonMultiple(Elevations.Count, Latitudes.Count); // } // else // { return 0; } // return Out; //} //private static int LowecstCommonMultiple(int a, int b) //{ // int num1, num2; // if (a > b) // { // num1 = a; num2 = b; // } // else // { // num1 = b; num2 = a; // } // for (int i = 1; i < num2; i++) // { // if ((num1 * i) % num2 == 0) // { // return i * num1; // } // } // return num1 * num2; //} } }