def sun_azimuth(latitude, longitude, date_time, elevacion_msnm): '''Azimuth is reckoned with zero corresponding to south. Positive azimuth estimates correspond to estimates east of south; negative estimates are west of south.''' azi = ps.GetAzimuth( latitude, longitude, date_time, elevation=elevacion_msnm) + 360 #GetAzimuth(latitude_deg, longitude_deg, utc_datetime, elevation = 0) return azi
def trackSunPosition(): global logfileDirect ser.write("<t>") latitudePB = 51.7 longitudePB = 8.7 elevationPB = 170 try: while True: date = datetime.datetime.utcnow() sunAlt = solar.GetAltitude(latitudePB, longitudePB, date, elevation=elevationPB) sunAzi = solar.GetAzimuth(latitudePB, longitudePB, date, elevation=elevationPB) sunAziCor = (-sunAzi + 180) % 360 strAlt = "{:.2f}".format(sunAlt) strAzi = "{:.2f}".format(sunAziCor) steps = int(sunAziCor / 0.225) print("Sun is at " + strAlt + " degree altitude and " + strAzi + " degree azimuth") msg = "" while msg.find("W4P") == -1: msg = recvFromArduino() ser.write("<" + str(steps) + "," + str(sunAlt) + ">") msg = "" while msg.find("MDone") == -1: msg = recvFromArduino() if msg.find("MDone") == -1: parts = re.split('\s|(?<!\d)[,.](?!\d)', msg) aziArd = float(parts[0]) eleArd = float(parts[1]) rVal = float(parts[2]) gVal = float(parts[3]) bVal = float(parts[4]) xpin = float(parts[5]) datetimeStr = datetime.datetime.now().strftime( '%Y/%m/%d %H:%M:%S') logfileDirect.write(datetimeStr + ",") logfileDirect.write( str(aziArd) + "," + str(eleArd) + "," + str(rVal) + "," + str(gVal) + "," + str(bVal) + "," + str(xpin)) logfileDirect.write("\n") print("The position of the arduino is: Altitude = " + str(eleArd) + ", Azimuth = " + str(aziArd)) print("The irradiaton values are: RED = " + str(rVal) + ", GREEN = " + str(gVal) + ", BLUE = " + str(bVal)) print("---------------------------------") time.sleep(10) except KeyboardInterrupt: ser.write("<e>")
def get_sza_azi(lat, lon, datetime): """ Program wrapper for pysolar to get the solar zenith angle and the solar azimuth angle can use inputs of list or numpy arrays require input of lat,lon,datetime """ import Pysolar.solar as sol try: n = len(lat) except TypeError: lat = [lat] lon = [lon] datetime = [datetime] n = len(lat) sza = [] azi = [] for i in range(n): sza.append(90.0 - sol.GetAltitude(lat[i], lon[i], datetime[i])) azi.append(sol.GetAzimuth(lat[i], lon[i], datetime[i])) return sza, azi
def calcPoints(origin, destination, flight_mins, departure_time): total_mins_left = 0 total_mins_right = 0 total_mins_night = 0 has_seen_sunset = False has_seen_sunrise = False mins_to_first_sunset = 0 mins_to_first_sunrise = 0 sunset_left_right = None sunrise_left_right = None from_lat, from_lon = origin to_lat, to_lon = destination number_points = flight_mins # num minutes point_time = departure_time # departure time in UTC gd = Geodesic.WGS84.Inverse(from_lat, from_lon, to_lat, to_lon) line = Geodesic.WGS84.Line(gd['lat1'], gd['lon1'], gd['azi1']) bearing = None solar_altitude = 0 last_solar_altitude = 0 points = [] for i in range(number_points + 1): point = line.Position(gd['s12'] / number_points * i) if ((i + 1) < number_points): # calculat bearing point2 = line.Position(gd['s12'] / number_points * i + 1) # bearing is degrees from north bearing = calcBearing((point['lat2'], point['lon2']), (point2['lat2'], point2['lon2'])) else: pass # use last calculated bearing last_solar_altitude = solar_altitude # keep track of last solar altitude (work out if sun setting or rising) solar_altitude = solar.GetAltitude(point['lat2'], point['lon2'], point_time) solar_azimuth = solar.GetAzimuth(point['lat2'], point['lon2'], point_time) # degrees from south if (solar_azimuth < 0): solar_azimuth_from_north = (180 + abs(solar_azimuth)) % 360 else: solar_azimuth_from_north = (180 - solar_azimuth) % 360 sun_east_west = "" # print("----------------- ") # print("minute = " + str(i)) # print("lat,lon = " + str((point['lat2'], point['lon2']))) # print("sun alt = " + str(solar_altitude)) # print("azimuth (from south) = " + str(solar_azimuth)) # print("azimuth (from north) = " + str(solar_azimuth_from_north)) # print("bearing (from north) = " + str(bearing)) point_values = {} point_values['min'] = i point_values['lat'] = round(point['lat2'], 4) point_values['lng'] = round(point['lon2'], 4) point_values['sun_alt'] = round(solar_altitude, 2) # point_values['azimuth_from_south'] = round(solar_azimuth, 2) point_values['azimuth_from_north'] = round(solar_azimuth_from_north, 2) point_values['bearing_from_north'] = round(bearing, 2) # sun postion if ((solar_azimuth >= 0) and (solar_azimuth <= 180)) or (solar_azimuth < -180): sun_east_west = "east" if ((solar_azimuth < 0) and (solar_azimuth > -180)) or (solar_azimuth > 180): sun_east_west = "west" #print("sun pos = " + sun_east_west) point_values['sun_east_west'] = sun_east_west # sun position from bearing of plane if (bearing > solar_azimuth_from_north): if (abs(bearing - solar_azimuth_from_north) < 180): sun_side = "left" else: sun_side = "right" else: if (abs(solar_azimuth_from_north - bearing) < 180): sun_side = "right" else: sun_side = "left" # store which side sun is on point_values['sun_side'] = sun_side # calculate total minutes left or right if (solar_altitude > 0.0): if (sun_side == 'left'): total_mins_left = total_mins_left + 1 else: total_mins_right = total_mins_right + 1 # night or day? # http://www.timeanddate.com/worldclock/aboutastronomy.html sunset_max_alt = 6.0 # 12 is correct but use 6 if (solar_altitude > sunset_max_alt): #print("time of day: day") point_values['tod'] = 'day' if not has_seen_sunrise: mins_to_first_sunrise = mins_to_first_sunrise + 1 if not has_seen_sunset: mins_to_first_sunset = mins_to_first_sunset + 1 if (solar_altitude > 0.0) and (solar_altitude <= sunset_max_alt): if (i <= 1): # not sure if its rising or setting # print("time of day: night") point_values['tod'] = 'night' if not has_seen_sunrise: mins_to_first_sunrise = mins_to_first_sunrise + 1 if not has_seen_sunset: mins_to_first_sunset = mins_to_first_sunset + 1 else: if (last_solar_altitude > solar_altitude): #print("time of day: sunset") point_values['tod'] = 'sunset' has_seen_sunset = True sunset_left_right = sun_side if not has_seen_sunrise: mins_to_first_sunrise = mins_to_first_sunrise + 1 else: #print("time of day: sunrise") point_values['tod'] = 'sunrise' has_seen_sunrise = True sunrise_left_right = sun_side if not has_seen_sunset: mins_to_first_sunset = mins_to_first_sunset + 1 if (solar_altitude < 0.0): #print("time of day: night") point_values['tod'] = 'night' total_mins_night = total_mins_night + 1 if not has_seen_sunrise: mins_to_first_sunrise = mins_to_first_sunrise + 1 if not has_seen_sunset: mins_to_first_sunset = mins_to_first_sunset + 1 points.append(point_values) point_time = point_time + datetime.timedelta(0, 60) # add 1 minute data = {} data['points'] = points # print stats #print("-------------") #print("Flight Stats:") #print("total_min_left = " + str(total_mins_left) + " (" + str(round((float(total_mins_left) / float(flight_mins)) * 100)) + "%)") #print("total_mins_right = " + str(total_mins_right) + " (" + str(round((float(total_mins_right) / float(flight_mins)) * 100)) + "%)") #print("total_mins_night = " + str(total_mins_night) + " (" + str(round((float(total_mins_night) / float(flight_mins)) * 100)) + "%)") flight_stats = {} flight_stats['total_minutes'] = flight_mins flight_stats['total_minutes_left'] = total_mins_left flight_stats['total_minutes_right'] = total_mins_right flight_stats['percent_left'] = round( (float(total_mins_left) / float(flight_mins)) * 100) flight_stats['percent_right'] = round( (float(total_mins_right) / float(flight_mins)) * 100) flight_stats['total_minutes_night'] = total_mins_night flight_stats['percent_night'] = round( (float(total_mins_night) / float(flight_mins)) * 100) flight_stats['mins_to_first_sunrise'] = 0 flight_stats['sunrise_left_right'] = "" if (mins_to_first_sunrise < flight_mins): flight_stats['mins_to_first_sunrise'] = mins_to_first_sunrise flight_stats['sunrise_left_right'] = sunrise_left_right flight_stats['mins_to_first_sunset'] = 0 flight_stats['sunset_left_right'] = "" if (mins_to_first_sunset < flight_mins): flight_stats['mins_to_first_sunset'] = mins_to_first_sunset flight_stats['sunset_left_right'] = sunset_left_right # make jsonp object data = {} data['flight_points'] = points data['flight_stats'] = flight_stats print json.dumps(data)
def _compute_sun_azimuth(latitude_deg, longitude_deg, time): return solar.GetAzimuth(latitude_deg, longitude_deg, time)