Esempio n. 1
0
 def get_timezone_offset(self) -> Optional[str]:
     zipcode = self.get_rich_zipcode()
     if zipcode is None:
         return None
     tf = TimezoneFinder()
     longitude = zipcode['Longitude']
     latitude = zipcode['Latitude']
     timezone_name = tf.timezone_at(lng=longitude, lat=latitude)
     if timezone_name is None:
         return None
     return datetime.datetime.now(pytz.timezone(timezone_name)).strftime('%z')
Esempio n. 2
0
def getTimeZone( location ):
    from timezonefinder import TimezoneFinder

    tf = TimezoneFinder( )

    if isinstance( location, str ):
        location = getLocation( location )
    elif not isinstance( location, RPNLocation ):
        raise ValueError( 'location name or location object expected' )

    timezone_name = tf.timezone_at( lat = location.getLat( ), lng = location.getLong( ) )

    if timezone_name is None:
        timezone_name = tf.closest_timezone_at( lat = location.getLat( ),
                                                lng = location.getLong( ) )

    return timezone_name
 def process_request(self, request):
     user_time_zone = request.session.get('user_time_zone', None)
     user_time_zone = None
     try:
         if user_time_zone is None:
             ip = get_real_ip(request)
             if ip is None:
                 tf = TimezoneFinder()
                 print
                 point = (request.user.userdata.address.longditude,request.user.userdata.address.latitude)
                 user_time_zone = tf.timezone_at(*point)
                 request.session['user_time_zone'] = user_time_zone
             else :
                 freegeoip_response = requests.get('http://freegeoip.net/json/{0}'.format())
                 freegeoip_response_json = freegeoip_response.json()
                 user_time_zone = freegeoip_response_json['time_zone']
                 if user_time_zone:
                     request.session['user_time_zone'] = user_time_zone
         timezone.activate(pytz.timezone(user_time_zone))
     except:
         pass
     return None
def filterhits(camera, data):
    # timezones
    tf = TimezoneFinder()
    zone = tf.timezone_at(lng=camera.lon, lat=camera.lat)

    timestamps = []
    for record in data:
        timestamps.append(record['submission']['image']['timestamp'])

    instance = {}
    for timestamp in set(timestamps):
        instance[timestamp] = {'vehicles': set(), 'bikes': set(), 'people': set()}
        for record in data:
            if record['submission']['image']['timestamp'] == timestamp:
                worker = record['submission']['workerId']
                bbox = Bbox(record['x1'], record['y1'], record['x2'], record['y2'], worker)
                # some have w = 0 or h = 0
                if bbox.w * bbox.h > 0:
                    instance[timestamp][record['type']].add(bbox)

    with open('camera_{}_points.csv'.format(camera.id), 'w') as f:
        f.write('year,month,day,hour,minute,type,count,x,y,url,url2,y,m,d,h,mi,camw,camh\n')
        for timestamp in sorted(instance.keys()):
            date, time = timestamp.split('T')
            y, m, d = date.split('-')
            h, mi, s = time.split(':')
            ts = timestamp.replace(':', '').replace('T', '_').replace('-', '')
            dt = datetime.datetime(year=int(y), month=int(m), day=int(d), hour=int(h), minute=int(mi), second=int(s), tzinfo=utc)
            lt = local_time(dt, zone)
            url = 'http://amos.cse.wustl.edu/mturk/reviewOutlines/{}/{}'.format(camera.id, ts)
            url2 = 'http://amos.cse.wustl.edu/image/{}/{}.jpg'.format(camera.id, ts)
            for htype in instance[timestamp].keys():
                records = resolve_duplicates(list(instance[timestamp][htype]))
                for xx, yy in records:
                    f.write('{yl},{ml},{dl},{hl},{mil},{tp},{cnt},{xx},{yy},{url},{url2},{y},{m},{d},{h},{mi},{camw},{camh}\n'
                            .format(yl=lt.year, ml=lt.month, dl=lt.day, hl=lt.hour, mil=lt.minute, y=y, m=m,
                                    d=d, h=h, mi=mi, tp=htype, cnt=len(records), xx=xx, yy=yy, url=url, url2=url2,
                                    camw=camera.w, camh=camera.h))
Esempio n. 5
0
import requests
import json
import timezonefinder

# req = requests.get('http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2&username=demo')
# req = req.json()
# print(req)

from timezonefinder import TimezoneFinder

TimezoneFinder.using_numba()  # this is a static method returning True or False

tf = TimezoneFinder()
# tf = TimezoneFinder(in_memory=True) # to use the faster "in-memory" mode
# tf = TimezoneFinder(bin_file_location='path/to/files') # to use data files from another location

longitude, latitude = 13.358, 52.5061
print(tf.timezone_at(lng=55.028395, lat=61.646646))  # returns 'Europe/Berlin'
print(tf.certain_timezone_at(lng=longitude,
                             lat=latitude))  # returns 'Europe/Berlin'
Esempio n. 6
0
File: bot.py Progetto: xuanb/msbot
    def on_chat_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance(msg)
        print('Chat:', content_type, chat_type, chat_id)
        if content_type != 'text':
            return self.sendMessage(chat_id, self.data['confused_msg'], reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
        text = msg['text'].lower()
        user = self.db.users.find_one({'chat_id':chat_id})
        if not chat_id in self.current_users:
            self.setUser (chat_id, msg['from'])
            if user:
                self.current_users[chat_id].mapUser(user)
                self.current_users[chat_id].reset = False
            elif not user and self.current_users[chat_id].reset and text not in self.data['start']:
                return self.restart(chat_id)
        if text in self.data['start'] or msg['message_id'] == 0:
            self.current_users[chat_id].reset = True
            self.current_users[chat_id].language_setting_msg = self.language_setting(chat_id)
        elif text in self.data['settings']:
            if not self.current_users[chat_id].reset:
                markup = ReplyKeyboardMarkup(keyboard=self.keyboard_two_col(self.data['setting_keyboard'][self.current_users[chat_id].language]),
                    resize_keyboard=True, one_time_keyboard=True)
                text_msg = self.data['setting_msg'][self.current_users[chat_id].language]
                self.sendMessage(chat_id, text_msg, reply_markup=markup)
            else:
                return self.restart(chat_id)
        elif text in self.data['get_exercise']:
            self.sendExercise(list(self.db.users.find({'chat_id': chat_id})))
        elif text in self.data['get_question']:
            self.questionGathering(list(self.db.users.find({'chat_id': chat_id})))
        elif text in self.data['statistics']:
            text_msg = {}
            en_start = "You started to use the service on {:%d, %b %Y}\n".format(user['_id'].generation_time)
            fi_start = "Aloit käyttää palvelua {:%d, %b %Y}\n".format(user['_id'].generation_time)
            en_recieved_exercises = "You have recieved {0} exercises.\nYou have done {1} exercises.\nAmong the done exercises, you have trained your:\ncore: {2} times\nlegs: {3} times\narms: {4} times\nhands: {5} times.\nAnd there were:\n{6} stretching exercises\n{7} balance exercises\n{8} strength exercises".format(self.db[str(chat_id)].count(),
                                        user['done'], user['core'], user['legs'], user['arms'], user['hands'],
                                        user['stretching'], user['balance'], user['strength'])
            fi_recieved_exercises = "Olet saanut {0} harjoitusta.\nOlet tehnyt {1} harjoitusta.\nTekemäsi harjoitukset ovat treenanneet:\nkeskikroppaa: {2} krt\njalkoja: {3} krt\nkäsivarsia: {4} krt\nkäsiä: {5} krt.\nHarjoitustyypit:\n{6} kpl venyttely\n{7} kpl tasapaino\n{8} kpl voima".format(self.db[str(chat_id)].count(),
                                        user['done'], user['core'], user['legs'], user['arms'], user['hands'],
                                        user['stretching'], user['balance'], user['strength'])
            en_goal = "\nYour current goal is: " + user['goals'][0]['en']
            fi_goal = "\nTämänhetkinen tavoitteesi on: " + user['goals'][0]['fi']

            text_msg['en'] = en_start + en_recieved_exercises + en_goal
            text_msg['fi'] = fi_start + fi_recieved_exercises + fi_goal
            self.sendMessage(chat_id, text_msg[self.current_users[chat_id].language], reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
        elif text in self.data['new_goal'] or msg['text'] == self.data['checkup_keyboard'][self.current_users[chat_id].language][1]:
            self.current_users[chat_id].goal_setting_msg = self.goal_setting(chat_id, self.current_users[chat_id].lowerbody_score, self.current_users[chat_id].upperbody_score)
        elif text in self.data['new_time'] or msg['text'] == self.data['checkup_keyboard'][self.current_users[chat_id].language][2]:
            self.current_users[chat_id].times_setting_msg = self.times_setting(chat_id)
        elif text in self.data['new_condition'] or msg['text'] == self.data['checkup_keyboard'][self.current_users[chat_id].language][3]:
            self.current_users[chat_id].lowerbody_setting_msg = self.lowerbody_setting(chat_id)
        elif text in self.data['new_language']:
            self.current_users[chat_id].language_setting_msg = self.language_setting(chat_id)
        elif text in self.data['new_location']:
            self.current_users[chat_id].location_setting_msg = self.location_setting(chat_id)
        elif msg['text'] == self.data['checkup_keyboard'][self.current_users[chat_id].language][0]:
            text_msg = self.data['checkup_continue_response'][self.current_users[chat_id].language]
            self.sendMessage(chat_id, text_msg, reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
        elif msg['message_id'] - self.current_users[chat_id].language_setting_msg < 5 and text in ['english','finnish']:
            self.current_users[chat_id].language = text[:2]
            self.current_users[chat_id].language_setting_msg = -1000
            if self.current_users[chat_id].reset:
                self.current_users[chat_id].location_setting_msg = self.location_setting(chat_id)
            else:
                text_msg = self.data['language_response'][text[:2]]
                self.sendMessage(chat_id, text_msg, reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
                self.updateUserDB(chat_id)
        elif msg['message_id'] - 1 == self.current_users[chat_id].location_setting_msg:
            g = geocoder.google(text)
            if len(g.latlng) == 0:
                text_msg = self.data['location_fail_response'][self.current_users[chat_id].language]
                self.sendMessage(chat_id, text_msg)
                self.current_users[chat_id].location_setting_msg = self.location_setting(chat_id)
                return
            tf = TimezoneFinder()
            self.current_users[chat_id].location = [g.latlng[1], g.latlng[0]]
            point = (g.latlng[1], g.latlng[0])
            self.current_users[chat_id].timezone = tf.timezone_at(*point)
            if self.current_users[chat_id].reset:
                self.current_users[chat_id].lowerbody_setting_msg = self.lowerbody_setting(chat_id)
            else:
                markup = ReplyKeyboardMarkup(keyboard=[
                    [self.data['time_keyboard'][self.current_users[chat_id].language]],
                ],resize_keyboard=True,one_time_keyboard=True)
                text_msg = self.data['location_response'][self.current_users[chat_id].language]
                self.sendMessage(chat_id, text_msg, reply_markup=markup)
                self.updateUserDB(chat_id)
        elif msg['message_id'] - self.current_users[chat_id].lowerbody_setting_msg < 5 and msg['text'] in self.data['lowerbody_keyboard'][self.current_users[chat_id].language]:
            self.current_users[chat_id].lowerbody_score = self.data['lowerbody_keyboard'][self.current_users[chat_id].language].index(msg['text'])
            self.current_users[chat_id].lowerbody_setting_msg = -1000
            self.current_users[chat_id].upperbody_setting_msg = self.upperbody_setting(chat_id)
        elif msg['message_id'] - self.current_users[chat_id].upperbody_setting_msg < 5 and msg['text'] in self.data['upperbody_keyboard'][self.current_users[chat_id].language]:
            self.current_users[chat_id].upperbody_score = self.data['upperbody_keyboard'][self.current_users[chat_id].language].index(msg['text'])
            self.current_users[chat_id].upperbody_setting_msg = -1000
            if self.current_users[chat_id].reset:
                self.current_users[chat_id].times_setting_msg = self.times_setting(chat_id)
            else:
                text_msg = self.data['condition_response'][self.current_users[chat_id].language]
                self.sendMessage(chat_id, text_msg,reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
                self.updateUserDB(chat_id)
        elif msg['message_id'] - self.current_users[chat_id].times_setting_msg < 5 and text.isdigit():
            self.current_users[chat_id].times = int(text)
            self.current_users[chat_id].times_setting_msg = -1000
            self.current_users[chat_id].time_setting_msg = self.time_setting(chat_id)
            self.current_users[chat_id].timeslots = []
        elif msg['message_id'] - self.current_users[chat_id].time_setting_msg < 10:
            try:
                naive = datetime.strptime ("2016-4-27 " + msg['text'], "%Y-%m-%d %H:%M")
            except ValueError:
                text_msg = self.data['time_fail_response'][self.current_users[chat_id].language]
                return self.sendMessage(chat_id, text_msg)
            local = pytz.timezone (self.current_users[chat_id].timezone)
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone (pytz.utc)
            self.current_users[chat_id].timeslots.append(utc_dt.strftime("%H"))
            if len(self.current_users[chat_id].timeslots) == self.current_users[chat_id].times:
                self.current_users[chat_id].time_setting_msg = - 1000
                if self.current_users[chat_id].reset:
                    self.current_users[chat_id].goal_setting_msg = self.goal_setting(chat_id, self.current_users[chat_id].lowerbody_score, self.current_users[chat_id].upperbody_score)
                else:
                    text_msg = self.data['time_response'][self.current_users[chat_id].language]
                    self.sendMessage(chat_id, text_msg, reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
                    self.updateUserDB(chat_id)
        elif msg['message_id'] - 1 == self.current_users[chat_id].goal_setting_msg:
            goal = self.db.goals.find_one({self.current_users[chat_id].language: msg['text']})
            if not goal:
                text_msg = self.data['goal_fail_response'][self.current_users[chat_id].language]
                self.sendMessage(chat_id, text_msg)
                self.current_users[chat_id].goal_setting_msg = self.goal_setting(chat_id, self.current_users[chat_id].lowerbody_score, self.current_users[chat_id].upperbody_score)
                return
            goal['timestamp'] = datetime.now()
            self.current_users[chat_id].timestamp = datetime.now()
            self.current_users[chat_id].goals.insert(0,goal)
            if self.current_users[chat_id].reset:
                text_msg = self.data['setup_done_response'][self.current_users[chat_id].language]
            else:
                text_msg = self.data['goal_response'][self.current_users[chat_id].language] + text
            self.sendMessage(chat_id, text_msg,reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
            self.updateUserDB(chat_id)
        elif msg['text'] in self.data['exercise_keyboard'][self.current_users[chat_id].language]:
            exercise = list(self.db[str(chat_id)].find().limit(1).sort([('_id',-1)]))[0]
            answered_timestamp = datetime.now()
            if msg['text'] == self.data['exercise_keyboard'][self.current_users[chat_id].language][0]:
                done = 1
                self.db.users.find_one_and_update({'chat_id': chat_id},{ '$inc': {
                            'strength'      : exercise['strength'],
                            'stretching'    : exercise['stretching'],
                            'balance'       : exercise['balance'],
                            'core'          : exercise['core'],
                            'hands'         : exercise['hands'],
                            'arms'          : exercise['arms'],
                            'legs'          : exercise['legs'],
                            'done'          : 1} })
                self.sendMessage(chat_id, self.doing_exercise_response(self.current_users[chat_id].language),reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
                self.sendMessage(chat_id, self.data['exercise_done_response'][self.current_users[chat_id].language])
            else:
                done = 0
                self.sendMessage(chat_id, self.skip_exercise_response(self.current_users[chat_id].language),reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
            self.db[str(chat_id)].update_one({'_id': exercise['_id']}, {'$set':{'answered_timestamp': answered_timestamp, 'done': done}})
            del self.current_users[chat_id]
        elif msg['text'] in self.data['question_keyboard'][self.current_users[chat_id].language]:
            if chat_id in self.users_to_send_msg:
                answer = {}
                answer['chat_id'] = chat_id
                answer['question_content'] = self.users_to_send_msg[chat_id]['question_msg']['text']
                answer['answer'] = msg['text']
                answer['question_id'] = self.users_to_send_msg[chat_id]['question']['_id']
                answer['timestamp'] = datetime.now()
                self.db.answers.insert_one(answer)
                del self.users_to_send_msg[chat_id]
            self.sendMessage(chat_id, self.data['question_resonse'][self.current_users[chat_id].language] + u'\U0001f44d', reply_markup=self.defaultMarkup[self.current_users[chat_id].language])
            del self.current_users[chat_id]
        else:
            response_msg = self.random_response(text)
            self.sendMessage(chat_id, response_msg)
Esempio n. 7
0
def do_the_things(lat, lon, chagdays=2, offset=0):
    tf = TimezoneFinder()
    tzname = tf.timezone_at(lng=lon, lat=lat)
    try:
        tz = pytz.timezone(tzname)
    except pytz.exceptions.UnknownTimeZoneError:
        tz = pytz.timezone('UTC')

    now = datetime.datetime.now(tz=tz)
    if offset is not None:
        now += datetime.timedelta(minutes=offset)
    noon = tz.localize(
        datetime.datetime(year=now.year,
                          month=now.month,
                          day=now.day,
                          hour=12,
                          minute=30))
    today = now.date()
    tomorrow = today + datetime.timedelta(days=1)

    # Get Hebrew calendar dates
    hebtoday = hebrew.from_gregorian(today.year, today.month, today.day)
    hebtomorrow = hebrew.from_gregorian(tomorrow.year, tomorrow.month,
                                        tomorrow.day)
    hebmonthtoday = hebrew_monthname(hebtoday)
    hebmonthtomorrow = hebrew_monthname(hebtomorrow)
    omertoday = omer_day(hebtoday)
    omertonight = omer_day(hebtomorrow)

    # Set up ephem info to determine sunset and nightfall
    herenow = ephem.Observer()
    herenow.lat, herenow.lon = lat * ephem.pi / 180, lon * ephem.pi / 180
    herenow.date = ephem.Date(now.astimezone(pytz.utc))
    herenoon = ephem.Observer()
    herenoon.lat, herenoon.lon = lat * ephem.pi / 180, lon * ephem.pi / 180
    herenoon.date = ephem.Date(noon.astimezone(pytz.utc))
    sun = ephem.Sun()

    # Determine "set" and "dark" for today (may be in the past)
    try:
        todayrise_eph = herenoon.previous_rising(sun)
        todayrise = pytz.utc.localize(todayrise_eph.datetime()).astimezone(tz)
        todayrise_txt = todayrise.isoformat(timespec='seconds')
        tonightset_eph = herenoon.next_setting(sun)
        tonightset = pytz.utc.localize(
            tonightset_eph.datetime()).astimezone(tz)
        tonightset_txt = tonightset.isoformat(timespec='seconds')
    except ephem.NeverUpError:
        todayrise_txt = 'downallday'
        tonightset_txt = 'downallday'
    except ephem.AlwaysUpError:
        todayrise_txt = 'upallday'
        tonightset_txt = 'upallday'
    oldhorizon = herenoon.horizon
    oldpressure = herenoon.pressure
    herenoon.pressure = 0
    # All horizon math is from top of sun disk
    # We need to take into account sun's radius, averaging .266 degrees
    herenoon.horizon = "-8.233"  # middle of sun 8.5 deg
    try:
        tonightdark_eph = herenoon.next_setting(sun)
        tonightdark = pytz.utc.localize(
            tonightdark_eph.datetime()).astimezone(tz)
        tonightdark_txt = tonightdark.isoformat(timespec='seconds')
    except ephem.NeverUpError:
        tonightdark_txt = 'alwaysdark'
    except ephem.AlwaysUpError:
        tonightdark_txt = 'none'
    herenoon.horizon = oldhorizon
    herenoon.pressure = oldpressure

    # Status of sun, handle no-rise/no-set cases first
    if todayrise_txt == "upallday":
        sunnow = "up"
    elif (todayrise_txt == 'downallday'
          and (tonightdark == 'alwaysdark' or tonightdark < now)):
        sunnow = "down"
    elif todayrise_txt == 'downallday':
        sunnow = "twilight"
    # normal cases
    elif todayrise > now:
        sunnow = "notyetup"
    elif tonightset > now:
        sunnow = "up"
    elif (tonightdark_txt == 'none' or tonightdark > now):
        sunnow = "twilight"
    else:
        sunnow = "down"

    # Is it Shabbat or a holiday?
    shabbat_or_holiday_today = (today.isoweekday() == 6)
    if (jewish_holiday(date=hebtoday, chagdays=chagdays)):
        shabbat_or_holiday_today = True
    shabbat_or_holiday_tonight = (today.isoweekday() == 5)
    if (jewish_holiday(date=hebtomorrow, chagdays=chagdays)):
        shabbat_or_holiday_tonight = True

    # Combine hebdate logic and shabbat/holiday logic with sun up/down logic
    if (sunnow == "notyetup" or sunnow == "up"):
        shabbat_or_holiday_now = shabbat_or_holiday_today
        hebrew_date_now = "{} {}, {}".format(hebtoday[2], hebmonthtoday,
                                             hebtoday[0])
        omernow = omertoday
    elif (sunnow == "down"):
        shabbat_or_holiday_now = shabbat_or_holiday_tonight
        hebrew_date_now = "{} {}, {}".format(hebtomorrow[2], hebmonthtomorrow,
                                             hebtomorrow[0])
        omernow = omertonight
    elif (sunnow == "twilight"):
        shabbat_or_holiday_now = (shabbat_or_holiday_today
                                  or shabbat_or_holiday_tonight)
        hebrew_date_now = "indeterminate"
        if omertoday > 0 and omertonight > 0:
            omernow = omertoday + 0.5
        else:
            omernow = 0
    else:
        raise ValueError("How is the sun not up or down or twilight?")

    # time for output
    to_return = {}
    to_return["results"] = {
        "now":
        now.astimezone(tz).isoformat(timespec='seconds'),
        "sunrise":
        todayrise_txt,
        "sunset":
        tonightset_txt,
        "jewish_twilight_end":
        tonightdark_txt,
        "sun_now":
        sunnow,
        "hebrew_date_today":
        "{} {}, {}".format(hebtoday[2], hebmonthtoday, hebtoday[0]),
        "hebrew_date_tonight":
        "{} {}, {}".format(hebtomorrow[2], hebmonthtomorrow, hebtomorrow[0]),
        "hebrew_date_now":
        hebrew_date_now,
        "shabbat_or_yom_tov_today":
        shabbat_or_holiday_today,
        "shabbat_or_yom_tov_tonight":
        shabbat_or_holiday_tonight,
        "shabbat_or_yom_tov_now":
        shabbat_or_holiday_now,
    }
    if omertoday > 0:
        to_return["results"]["omer_today"] = omertoday
    if omertonight > 0:
        to_return["results"]["omer_tonight"] = omertonight
    if omernow > 0:
        to_return["results"]["omer_now"] = omernow

    return json.dumps(to_return)
Esempio n. 8
0
    lat = float(argv[1])
    lon = float(argv[2])
except Exception as e:
    print(e)
    exit()

try:
    from timezonefinder import TimezoneFinder
except Exception as e:
    print(e)
    exit()

tz = TimezoneFinder()

try:
    tz_str = tz.timezone_at(lng=lon, lat=lat)

    if tz_str == None:
        #tz_str = tz.certain_timezone_at(lng=lon, lat=lat)
        tz_str = tz.closest_timezone_at(lng=lon, lat=lat)

    print(tz_str)

    if tz_str != None:
        cmd = [
            "sudo",
            "timedatectl",
            "set-timezone",
            tz_str,
        ]
        print(cmd)
Esempio n. 9
0
def gettimezone(latitude, longitude):
    tf = TimezoneFinder()
    return tf.timezone_at(lat=latitude, lng=longitude)
Esempio n. 10
0
from timezonefinder import TimezoneFinder

tf = TimezoneFinder()
lat = 32.23114
lon = -110.94548
print(tf.timezone_at(lng=lon, lat=lat))
Esempio n. 11
0
 def timezone(self):
     tf = TimezoneFinder()
     return tf.timezone_at(lng=self.longitude, lat=self.latitude)
Esempio n. 12
0
def interpolate_time(fflightLog):
    gpsEventDict = collections.OrderedDict(
    )  # stores position,altitude,video status, interpolation indicator with time as key
    gpsEventList = []
    rowCount = 0
    timestamp = 0
    seedTime = 0
    newTime = 0
    prevTime = 0
    nextTime = 0
    lastTime = 0
    timeDelta = 0
    takingVideo = '0'
    prevLat = 0.0
    nextLat = 0.0
    prevLong = 0.0
    nextLong = 0.0
    prevAlt = 0.0
    nextAlt = 0.0
    latitude = 0.0
    longitude = 0.0
    altitude = 0.0
    with open(fflightLog, 'rU') as log:

        header = next(log)  # Skip the header row

        previous = next(
            log)  # Save the initial data row for comparison with next row
        rowCount += 1
        rowFields = previous.split(',')
        latitude = float(rowFields[0])
        longitude = float(rowFields[1])
        utmPosition = utm.from_latlon(latitude, longitude)
        utmPositionX = utmPosition[0]
        utmPositionY = utmPosition[1]
        utmLatZone = utmPosition[2]
        utmLongZone = utmPosition[3]
        altitude = float(
            rowFields[2]) * 0.3048  # Convert altitude in feet to meters
        lYear = rowFields[11][0:4]
        lMonth = rowFields[11][5:7]
        lDay = rowFields[11][8:10]
        lhours = rowFields[11][11:13]
        lminutes = rowFields[11][14:16]
        lsecs = rowFields[11][17:19]
        lmsecs = rowFields[11][20:]
        lDate = lYear + '/' + lMonth + '/' + lDay
        lTime = lhours + ':' + lminutes + ':' + lsecs + '.' + lmsecs
        takingVideo = rowFields[37]
        timestamp = int(rowFields[43])
        interpolated = False  # The row is read directly from the file and is therefore not interpolated
        tf = TimezoneFinder()
        tzone = tf.timezone_at(lng=longitude, lat=latitude)

        prevTime = timestamp
        prevLat = latitude
        prevLong = longitude
        prevAlt = altitude

        # Store the log data for first row if video was enabled

        if takingVideo == '1':

            # Convert the timestamp to UTC time

            dateTimeStr = datetime.datetime.utcfromtimestamp(
                timestamp / 1000.0).strftime('%Y/%m/%d %H:%M:%S.%f')
            DateStr = dateTimeStr.split(' ')[0]
            TimeStr = dateTimeStr.split(' ')[1]

            # Store the first row of the log

            gpsEventList = [
                DateStr, TimeStr, latitude, longitude, altitude, takingVideo,
                interpolated, timestamp, UtmPositionX, UtmPositionY,
                UtmLatZone, UtmLongZone
            ]
            gpsEventDict[timestamp] = gpsEventList

        for row in log:  # Now start processing the rest of the log
            rowCount += 1
            rowFields = row.split(',')
            latitude = float(rowFields[0])
            longitude = float(rowFields[1])
            utmPosition = utm.from_latlon(latitude, longitude)
            utmPositionX = utmPosition[0]
            utmPositionY = utmPosition[1]
            utmLatZone = utmPosition[2]
            utmLongZone = utmPosition[3]
            altitude = float(rowFields[2]) * 0.3048
            lYear = rowFields[11][0:4]
            lMonth = rowFields[11][5:7]
            lDay = rowFields[11][8:10]
            lhours = rowFields[11][11:13]
            lminutes = rowFields[11][14:16]
            lsecs = rowFields[11][17:19]
            lmsecs = rowFields[11][20:]
            lDate = lYear + '/' + lMonth + '/' + lDay
            lTime = lhours + ':' + lminutes + ':' + lsecs + '.' + lmsecs
            takingVideo = rowFields[37]
            timestamp = int(rowFields[43])
            interpolated = False
            #print utmPositionX,utmPositionY,altitude
            if takingVideo == '1':

                # Always store the data from the row read from the log

                # Convert the timestamp to UTC time

                dateTimeStr = datetime.datetime.utcfromtimestamp(
                    timestamp / 1000.0).strftime('%Y/%m/%d %H:%M:%S.%f')
                DateStr = dateTimeStr.split(' ')[0]
                TimeStr = dateTimeStr.split(' ')[1]

                # Store the log data

                gpsEventList = [
                    DateStr, TimeStr, latitude, longitude, altitude,
                    takingVideo, interpolated, timestamp, utmPositionX,
                    utmPositionY, utmLatZone, utmLongZone
                ]
                gpsEventDict[timestamp] = gpsEventList

                # Start interpolation

                timeDelta = timestamp - prevTime
                latDelta = latitude - prevLat
                longDelta = longitude - prevLong
                altDelta = altitude - prevAlt

                if seedTime == 0:
                    seedTime = round((timestamp + 5) / 1000.0, 2) * 1000
                    newTime = seedTime

                # Normal sampling interval is 100ms. The interpolated sampling interval is 10 ms.
                # If the difference between the two times of interest is > 20 ms perform interpolation

                lastTime = prevTime
                if abs(timeDelta) > 10:
                    interpolated = True
                    newLat = prevLat
                    newLong = prevLong
                    newAlt = prevAlt
                    while newTime < timestamp:
                        timeFraction = (newTime - lastTime) / timeDelta
                        if latDelta > 0.0:
                            newLat += abs(latDelta) * timeFraction
                        elif latDelta < 0.0:
                            newLat -= abs(latDelta) * timeFraction
                        if longDelta > 0.0:
                            newLong += abs(longDelta) * timeFraction
                        elif longDelta < 0.0:
                            newLong -= abs(longDelta) * timeFraction
                        if altDelta > 0.0:
                            newAlt += abs(altDelta) * timeFraction
                        elif altDelta < 0.0:
                            newAlt -= abs(altDelta) * timeFraction
                        newTimeStr = datetime.datetime.utcfromtimestamp(
                            newTime / 1000.0).strftime('%Y/%m/%d %H:%M:%S.%f')
                        newDateStr = newTimeStr.split(' ')[0]
                        newTimeStr = newTimeStr.split(' ')[1]
                        newUtmPosition = utm.from_latlon(newLat, newLong)
                        newUtmPositionX = newUtmPosition[0]
                        newUtmPositionY = newUtmPosition[1]
                        newUtmLatZone = newUtmPosition[2]
                        newUtmLongZone = newUtmPosition[3]
                        gpsEventList = [
                            newDateStr, newTimeStr, newLat, newLong, newAlt,
                            takingVideo, interpolated, newTime,
                            newUtmPositionX, newUtmPositionY, newUtmLatZone,
                            newUtmLongZone
                        ]
                        gpsEventDict[newTime] = gpsEventList
                        lastTime = newTime
                        newTime += 10
                        #print '*',newUtmPositionX, newUtmPositionY, newAlt
                        #time.sleep(0.1)
                else:
                    newTime += 0
            prevTime = timestamp
            prevLat = latitude
            prevLong = longitude
            prevAlt = altitude

    return gpsEventDict, tzone
Esempio n. 13
0
def render_html(scene, geocoder, start, icons):
    trackpoints = scene.trackpoints

    if not trackpoints:
        print('EMPTY')
        return 'NOTHING'

    lat0 = trackpoints[0].latitude_degrees
    lon0 = trackpoints[0].longitude_degrees

    geocodes = geocoder.search(lat0, lon0)
    geocode = geocodes[0]

    from timezonefinder import TimezoneFinder
    tf = TimezoneFinder()
    name = tf.timezone_at(lng=lon0, lat=lat0)
    tz = pytz.timezone(name)
    utc = pytz.utc
    print(tz)
    for p in trackpoints:
        if p.time is not None:
            p.time = utc.localize(p.time).astimezone(tz)

    route = [[p.latitude_degrees, p.longitude_degrees] for p in trackpoints]
    #mileposts = []
    mileposts = list(insert_mileposts(trackpoints))

    icons.extend(
        {
            'lat': p.latitude_degrees,
            'lon': p.longitude_degrees,
            'label': '{:.0f} mi<br>{:%-I:%M} {}'.format(
                p.distance_meters * MILES_PER_METER,
                p.time,
                p.time.strftime('%p').lower(),
            ) if p.time else '{:.0f}'.format(
                p.distance_meters * MILES_PER_METER,
            ),
        }
        for p in mileposts
    )

    def compute_splits(trackpoints, mileposts):
        previous = trackpoints[0]
        for m in mileposts + [trackpoints[-1]]:
            meters = m.distance_meters - previous.distance_meters
            duration = m.time - previous.time if m.time else None
            yield Split(
                start = previous.time,
                end = m.time,
                duration = duration,
                meters = meters,
                mph = mph(meters, duration),
                elevation_meters = m.elevation_meters,
                elevation_gain_meters = (
                    m.elevation_gain_meters - previous.elevation_gain_meters
                    if previous else nan
                ),
                elevation_loss_meters = (
                    m.elevation_loss_meters - previous.elevation_loss_meters
                    if previous else nan
                ),
            )
            previous = m

    splits = list(compute_splits(trackpoints, mileposts))
    if trackpoints[0].time is None:
        duration = None
    else:
        duration = trackpoints[-1].time - trackpoints[0].time

    meters = trackpoints[-1].distance_meters
    miles = meters * MILES_PER_METER
    #mph = miles / duration.total_seconds() * 60 * 60

    # TODO: read only once instead of N times
    template_path = 'test.template.html'
    with open(template_path) as f:
        template_html = f.read()

    def to_url(cache_file_path):
        if not cache_file_path:
            return None
        return (cache_file_path
                .split('/')[-1]
                .replace('.xml', '.html'))

    template = SimpleTemplate(template_html)
    content = template.render(
        duration=duration,
        escale=FEET_PER_METER,
        icons=json.dumps(icons),
        route=json.dumps(route),
        miles=miles,
        mph=mph(meters, duration),
        nan=nan,
        next_url=to_url(scene.next),
        previous_url=to_url(scene.previous),
        splits=splits,
        start=start or trackpoints[0].time,
    )

    #print(splits)

    return content

    with open(output_path, 'w') as f:
        f.write(content)

    print(geocode)

    return Summary(
        geocode=geocode,
        start=trackpoints[0].time,
        miles=miles,
        url=output_path.name,
    )
    lambda t: t.strftime('%Y-%m-%d %H:%M'))

tf = TimezoneFinder()

dt_UTC = []  #create an empty list for the UTC times
dt_zone = []  #create an empty list for the zones, just to check

for i in range(df.shape[0]):

    raw_dat = df.iloc[i].tolist()
    #Get store positions of the lat and lon columns in the dataframe
    lat_pos = df.columns.get_loc("Latitude_(decimal_degrees)")
    lon_pos = df.columns.get_loc("Longitude_(decimal_degrees)")
    #pass lat and lon positions
    latitude, longitude = raw_dat[lat_pos], raw_dat[lon_pos]
    zone = tf.timezone_at(lng=longitude, lat=latitude)
    dt_zone.append(zone)
    print("converting to UTC for timezone: " + zone)
    local = pytz.timezone(zone)  #set local time zone string

    dt_pos = df.columns.get_loc("Local_Date_Time")
    naive = datetime.datetime.strptime(raw_dat[dt_pos], "%Y-%m-%d %H:%M")
    local_dt = local.localize(naive, is_dst=None)
    utc_dt = local_dt.astimezone(pytz.utc)
    dt_UTC.append(utc_dt.strftime("%Y-%m-%d %H:%M:%S"))
#make a columns for converted time and time zone

df["UTC"] = dt_UTC
df['zone'] = dt_zone
#Split the time into db friendly columns
df['UTC_Date_sampled_(YYYY-MM-DD)'] = df['UTC'].apply(lambda x: x).str.split(
Esempio n. 15
0
    def prepareCityForecast(self, res, data):
        if res != {}:
            tf = TimezoneFinder()
            #print(res)
            if res.get("list") and res["list"] != None:
                #print("there is a list of forecasts")
                forecasts = []
                for i in range(0, len(res["list"])):
                    forecast = {
                        "temp":
                        res["list"][i]["main"]["temp"],
                        "feels_like":
                        res["list"][i]["main"]["feels_like"],
                        "min_temp":
                        res["list"][i]["main"]["temp_min"],
                        "max_temp":
                        res["list"][i]["main"]["temp_max"],
                        "pressure":
                        res["list"][i]["main"]["pressure"],
                        "humidity":
                        res["list"][i]["main"]["humidity"],
                        "main":
                        res["list"][i]["weather"][0]["main"],
                        "description":
                        res["list"][i]["weather"][0]["description"],
                        "icon":
                        res["list"][i]["weather"][0]["icon"],
                        "dt":
                        res["list"][i]["dt"],
                        "date_text":
                        datetime.datetime.fromtimestamp(
                            res["list"][i]["dt"],
                            tz=timezone(
                                tf.timezone_at(lng=res["city"]["coord"]["lon"],
                                               lat=res["city"]["coord"]["lat"])
                            )).strftime("%m-%d-%Y %I:%M:%S %p %Z"),
                        "date_time":
                        datetime.datetime.fromtimestamp(
                            res["list"][i]["dt"],
                            tz=timezone(
                                tf.timezone_at(
                                    lng=res["city"]["coord"]["lon"],
                                    lat=res["city"]["coord"]["lat"]))),
                        "dt_txt":
                        res["list"][i]["dt_txt"]
                    }
                    forecasts.append(forecast)

                data.update({"forecasts": forecasts})

            if res.get("city") and res["city"] != None:
                data.update({
                    "latitude":
                    res["city"]["coord"]["lat"],
                    "longitude":
                    res["city"]["coord"]["lon"],
                    "local_timezone":
                    timezone(
                        tf.timezone_at(lng=res["city"]["coord"]["lon"],
                                       lat=res["city"]["coord"]["lat"])),
                    "country":
                    res["city"]["country"],
                    "sunrise":
                    datetime.datetime.fromtimestamp(
                        res["city"]["sunrise"],
                        tz=timezone(
                            tf.timezone_at(lng=res["city"]["coord"]["lon"],
                                           lat=res["city"]["coord"]["lat"]))),
                    "sunset":
                    datetime.datetime.fromtimestamp(
                        res["city"]["sunset"],
                        tz=timezone(
                            tf.timezone_at(lng=res["city"]["coord"]["lon"],
                                           lat=res["city"]["coord"]["lat"]))),
                    "timestamp":
                    datetime.datetime.now(tz=timezone(
                        tf.timezone_at(lng=res["city"]["coord"]["lon"],
                                       lat=res["city"]["coord"]["lat"])
                    )),  #.strftime("%A %B %d, %Y %I:%M:%S %p %Z"),
                    "name":
                    res["city"]["name"]
                })
Esempio n. 16
0
import pytz
from datetime import datetime, timezone, timedelta
from timezonefinder import TimezoneFinder

tf = TimezoneFinder()
lat, lon = -33.3804, -70.58017  # America/Santiago
tzName = tf.timezone_at(lng=lon, lat=lat)

print('tzName:', tzName)

remoteTz = pytz.timezone(tzName)
localTz = datetime.now().astimezone().tzinfo

nowHere = datetime.now(tz=localTz)
nowThere = datetime.now(tz=remoteTz)
print('now here: ', nowHere)
print('now there:', nowThere)

print('KOHEU.')
Esempio n. 17
0
#Reference :
#          :  https://pypi.python.org/pypi/timezonefinder   Python library to look up timezone from lat / long offline
#          :  https://pypi.python.org/pypi/pytz             World timezone definitions

import pytz
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
import datetime

try:
    geolocator = Nominatim()

    place = input('Enter city name : ')

    loc = geolocator.geocode(place)

    tf = TimezoneFinder()
    timezone = tf.timezone_at(lat=loc.latitude, lng=loc.longitude)

    time = datetime.datetime.now(pytz.timezone(timezone))
    print("Location :", loc.address, str(time).split('.')[0])

except Exception as e:
    print(str(e))
Esempio n. 18
0
from timezonefinder import TimezoneFinder

TimezoneFinder.using_numba()  # this is a static method returning True or False

tf = TimezoneFinder()
# or
tf = TimezoneFinder(in_memory=True)

longitude, latitude = 13.358, 52.5061
tf.timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Berlin'
tf.certain_timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Berlin'

longitude = 12.773955
latitude = 55.578595
tf.closest_timezone_at(lng=longitude,
                       lat=latitude)  # returns 'Europe/Copenhagen'

longitude = 42.1052479
latitude = -16.622686
tf.closest_timezone_at(lng=longitude,
                       lat=latitude,
                       delta_degree=2,
                       exact_computation=True,
                       return_distances=True,
                       force_evaluation=True)

tf.get_geometry(tz_name='Africa/Addis_Ababa', coords_as_pairs=True)
tf.get_geometry(tz_id=400, use_id=True)

# To maximize the chances of getting a result in a Django view it might look like:
def today_or_tomorrow_sunset(lat, lon):
    """determines if you should use todays or tomorrows sunset and gives the coordinates"""

    # **** Must give the api the 'local' sunset date in order to return the
    # day variable of 'today' or 'tomorrow'. ****

    current_date = datetime.date.today()
    current_date_str = current_date.strftime('%Y-%m-%d')

    #getting current utc for object comparison
    current_utc = datetime.datetime.utcnow()
    # import pdb; pdb.set_trace()

    #This doesn't always work...???
    #using tzwhere to get the timezone for given coordinates

    #ATTEMPTING TO USE DIFFERENT LIBRARY FOR TZ FINDER
    # tz = tzwhere.tzwhere()

    # try:
    #     tz_str = tz.tzNameAt(lat, lon)
    # except:
    #     raise NoTimeZoneAvailable('No Time Zone Available')

    # **************************** #
    # OH MAH GURD SO MUCH FASTER :)
    tf = TimezoneFinder()
    tz_str = tf.timezone_at(lng=lon, lat=lat)

    #using pytz to make object to calculate utc offset
    timezone = pytz.timezone(tz_str)

    tz_offset = timezone.utcoffset(current_utc)

    #datetime/date object according to the coordinates entered!
    current_local_time = current_utc + tz_offset
    current_local_date = current_local_time.date()

    #Find sunset time local to given airport
    sunset_time_today = return_sunset_time(lat, lon, current_local_date)

    #Creating datetime objects with strptime:
    #'2017-11-09T01:03:37+00:00' is the format for the sunet time.
    sunset_time_today_obj = datetime.datetime.strptime(
        sunset_time_today, '%Y-%m-%dT%H:%M:%S+00:00')

    #If Sunset has already passed, we want to give you tomorrow's time.
    #sunset_time greater means the sunset has NOT happened already, so use today.
    if sunset_time_today_obj > current_utc:
        sunset_time_obj = sunset_time_today_obj
        day = 'today'

    else:
        #Getting tomorrow's date:
        tomorrow = current_date + datetime.timedelta(days=1)
        #Getting sunset time using tomorrow's date
        sunset_time_tomorrow = return_sunset_time(lat, lon, tomorrow)
        #Setting the sunset time = tomorrow's sunset time
        sunset_time_obj = datetime.datetime.strptime(
            sunset_time_tomorrow, '%Y-%m-%dT%H:%M:%S+00:00')
        day = 'tomorrow'

    sunset_time_str = sunset_time_obj.strftime('%Y-%m-%d %H:%M:%S UTC')

    #To make it prettier, get rid of microseconds.
    current_local_time = current_local_time.replace(microsecond=0)
    current_local_time = current_local_time.time()

    sunset_time_obj = sunset_time_obj.replace(microsecond=0)

    local_sunset_time = sunset_time_today_obj + tz_offset
    local_sunset_time = local_sunset_time.time()

    return {
        'time': sunset_time_obj,
        'day': day,
        'sunset_str': sunset_time_str,
        'local_time': current_local_time,
        'local_tz': timezone,
        'local_sunset_time': local_sunset_time
    }
Esempio n. 20
0
def results():
    try:
        location_input = request.form.get('location')
    except:
        location_input = "Arcata, CA"

    try:
        size_input = float(request.form.get('size'))
    except:
        size_input = 3.5

    # creating location characteristics
    city, state = location_input.split(',')
    city = city.strip()
    state = state.strip()

    tf = TimezoneFinder()

    # convert location to latitude and longitude
    geolocator = Nominatim(user_agent="powerpredictor")
    geopy.geocoders.options.default_timeout = 60
    location = geolocator.geocode(location_input)
    latitude = location.latitude
    longitude = location.longitude
    tzone = tf.timezone_at(lng=longitude, lat=latitude)
    altitude = location.altitude

    # get NSRDB data for 2017
    nsrdb_data = nsrdb.get_nsrdb_data(latitude, longitude, config.nrel_api_key)

    # get weather forecast
    ds_data = ds.get_forecast(latitude, longitude, config.ds_key)

    # get pvlib irradiances
    start_time = ds_data['datetime'].min()
    end_time = ds_data['datetime'].max()
    pvlib_data = pv.get_pvlib_data(latitude, longitude, tzone, altitude, city,
                                   start_time, end_time)

    # merging forecast and nsrdb data
    ds_data['month'] = ds_data['datetime'].dt.month
    ds_data['day'] = ds_data['datetime'].dt.day
    ds_data['hour'] = ds_data['datetime'].dt.hour
    data = pd.merge(ds_data,
                    nsrdb_data,
                    on=['month', 'day', 'hour'],
                    how='left')

    # merging data with pvlib data
    pvlib_data['datetime'] = pvlib_data.index
    pvlib_data['datetime'] = pvlib_data['datetime'].dt.tz_localize(None)
    data = pd.merge(data,
                    pvlib_data[[
                        'datetime', 'GHI_pvlib', 'DNI_pvlib', 'DHI_pvlib',
                        'GHI_clearsky'
                    ]],
                    on='datetime')

    # getting predicted GHI
    results = pred.predict_ghi(data)

    # barchart with power available
    results['date'] = results['datetime'].dt.date
    results_by_date = results.groupby('date')

    # calculating energy as integral of power curve
    solar_energy = results_by_date.apply(
        lambda x: abs(trapz(x['hour'], x['GHI_pred'])))
    max_power = results_by_date.apply(lambda x: max(x['GHI_pred']))
    power_values = results_by_date.apply(lambda x: x[x['GHI_pred'] > 0])
    power_values.index = pd.to_datetime(power_values['datetime'])
    power_by_date = power_values.groupby('date')
    avg_power = power_by_date['GHI_pred'].mean()
    sys_energy = solar_energy * size_input * 0.77
    energy = pd.DataFrame({
        'solar_energy': solar_energy,
        'max_power': max_power,
        'avg_power': avg_power,
        'sys_energy': sys_energy
    })
    energy.index = pd.to_datetime(energy.index)
    energy['day_name'] = energy.index.day_name()
    energy['short_day'] = energy.index.strftime("%a").astype(str)

    # getting rid of erroneous values
    energy = energy.drop(energy.index[-1])
    energy = energy.drop(energy.index[0])

    # getting the plot
    plotscript, plotdiv = pm.make_plot(energy)

    # average power per day
    power_average = (energy['sys_energy'].mean() / 1000).round(2)

    return render_template('results.html',
                           MESSAGE_MID=power_average,
                           size_input=size_input,
                           plotscript=plotscript,
                           plotdiv=plotdiv,
                           latitude=latitude,
                           longitude=longitude)
Esempio n. 21
0
                            'urdb_label': urdb_label,
                            'net_metering_limit_kw': net_metering_limit
                        },
                        'Storage': {
                            'total_rebate_us_dollars_per_kwh': kwh_rebate
                        },
                    }
                }
            },
            'climate_zone': climate_zone,
            'weatherfile': weatherfile,
            'latitude': latitude,
            'longitude': longitude,
            'num_simulations': num_simulations,
            'timesteps_per_hour': timesteps_per_hour,
            'timezone': timezone_finder.timezone_at(lat=latitude,
                                                    lng=longitude)
        }

        # UUID tags the run based on the dictionary contents so don't overwrite
        uuid = getID(flatten_dict(template))
        urdb_beginning = urdb_label[:5]
        tag = \
            f"{location}-{tariff_name}-{urdb_beginning}-net-metering-" \
            f"{row['net metering']}-sched-{schedules_type}-{num_simulations}-" \
            f"sims-rebate-{kwh_rebate}-{uuid}".replace(' ', '-').lower()
        template['tag'] = tag
        fname = 'template-' + tag + '.json'
        with open(os.path.join(TEMPLATE_DIRECTORY, fname), 'w+') as output:
            print(f"writing {fname}...")
            json.dump(template, output, indent=2)
Esempio n. 22
0
def tz_at_coords(lat, lng):
    tf = TimezoneFinder()
    return tf.timezone_at(lng=lng, lat=lat)
Esempio n. 23
0
File: utils.py Progetto: imsure/lg
def gps2timezone(lat, lon):
    tf = TimezoneFinder()
    tz = tf.timezone_at(lng=lon, lat=lat)
    return tz
Esempio n. 24
0
    def module_task(self, params):
        self.logger.info('Starting Module for edinet baseline...')
        """CHECK INCONSISTENCIES IN params"""
        try:
            result_companyId = params['result_companyId']
            ts_to = params['ts_to']
            ts_from = params[
                'ts_from'] if 'ts_from' in params else date_n_month(
                    ts_to, -24)
            energyTypeList = params['type'] if 'type' in params else []
            save_data_debug = True if 'debug' in params and params[
                'debug'] else False
        except KeyError as e:
            raise Exception(
                'Not enough parameters provided to module: {}'.format(e))

        #####################################################################################################################################################################################
        """  LOAD DATA FROM MONGO MODELLING UNITS  """
        ######################################################################################################################################################################################

        self.logger.info('Extracting modelling_units from mongo')

        # setting variables for readability
        modelling_units_collection = self.config['mongodb'][
            'modelling_units_collection']
        weather_stations_collection = self.config['mongodb'][
            'weather_stations_collection']
        lon_lat_tz_dict = {}
        tf = TimezoneFinder(in_memory=True)
        self.logger.debug('Querying for weather station info in MongoDB')
        cursor = self.mongo[weather_stations_collection].find({})
        for station in cursor:
            lon_lat_tz_dict[station['stationId']] = {
                "lat":
                station['latitude'],
                "lon":
                station['longitude'],
                "tz":
                tf.timezone_at(lat=station['latitude'],
                               lng=station['longitude'])
            }
        cursor.close()
        tf = None
        device_key = {}
        stations = {}
        solar_station = {}
        self.logger.debug('Querying for modelling unit info in MongoDB')
        cursor = self.mongo[modelling_units_collection].find({})
        for item in cursor:

            if len(item['devices']) > 0 and item[
                    'stationId'] != "Unknown":  # to avoid empty list of devices
                for dev in item['devices']:

                    stations[dev['deviceId']] = item[
                        'stationId'] if 'stationId' in item else None
                    solar_station[dev['deviceId']] = item[
                        'solar_station'] if 'solar_station' in item else None
                    key_str = "{modelling}~{devices}~{lat}~{lon}~{tz}".format(
                        modelling=item['modellingUnitId'],
                        devices=item['devices'],
                        lat=lon_lat_tz_dict[item['stationId']]['lat'],
                        lon=lon_lat_tz_dict[item['stationId']]['lon'],
                        tz=lon_lat_tz_dict[item['stationId']]['tz'])
                    if dev['deviceId'] in device_key.keys():
                        device_key[dev['deviceId']].append(key_str)
                    else:
                        device_key[dev['deviceId']] = [key_str]
        cursor.close()
        self.logger.info('A mongo query process has loaded {} devices'.format(
            len(device_key.keys())))

        ######################################################################################################################################################################################
        """ CREATE INPUT DATA FROM HIVE TABLES """
        ######################################################################################################################################################################################
        # create a table to link devices with stations
        self.logger.debug('creating weather hive table')

        weather_stations_df = pd.DataFrame(data={
            "deviceId": list(stations.keys()),
            "stationId": list(stations.values())
        },
                                           columns=["deviceId", "stationId"])

        f_station = NamedTemporaryFile(delete=False, suffix='.csv')
        weather_stations_df.to_csv(f_station.name, header=None, index=None)

        call([
            "hadoop", "fs", "-mkdir", "-p", f_station.name,
            self.config['paths']['stations']
        ])
        call([
            "hadoop", "fs", "-copyFromLocal", f_station.name,
            self.config['paths']['stations']
        ])
        weather_stations = create_hive_module_input_table(
            self.hive,
            'edinet_weather_stations_table',
            self.config['paths']['stations'], [('deviceId', 'string'),
                                               ('stationId', 'string')],
            self.task_UUID,
            sep=",")
        self.context.add_clean_hive_tables(weather_stations)

        # create a table to link devices with solar_stations
        self.logger.debug('creating solar hive table')

        solar_stations_df = pd.DataFrame(data={
            "deviceId":
            list(solar_station.keys()),
            "stationId":
            list(solar_station.values())
        },
                                         columns=["deviceId", "stationId"])
        f_solar_station = NamedTemporaryFile(delete=False, suffix='.csv')
        solar_stations_df.to_csv(f_solar_station.name, header=None, index=None)

        call([
            "hadoop", "fs", "-mkdir", "-p", f_solar_station.name,
            self.config['paths']['solar_stations']
        ])
        call([
            "hadoop", "fs", "-copyFromLocal", f_solar_station.name,
            self.config['paths']['solar_stations']
        ])
        solar_stations = create_hive_module_input_table(
            self.hive,
            'edinet_solar_stations_table',
            self.config['paths']['solar_stations'], [('deviceId', 'string'),
                                                     ('stationId', 'string')],
            self.task_UUID,
            sep=",")
        self.context.add_clean_hive_tables(solar_stations)

        # create a table with the devices values
        self.logger.debug('creating input table')

        final_table_fields = [
            [x[0], x[1]] for x in self.config['hive']['final_table_fields']
        ]

        location = self.config['paths']['measures']

        input_table = create_hive_module_input_table(
            self.hive, self.config['hive']['job_table_name'], location,
            final_table_fields, self.task_UUID)

        #add input table to be deleted after execution
        self.context.add_clean_hive_tables(input_table)
        self.logger.debug('creating hive query')

        qbr = RawQueryBuilder(self.hive)
        total_select_joint = ", ".join([
            "{}.{}".format(x[2], x[0])
            for x in self.config['hive']['final_table_fields']
        ])
        sentence = """
            INSERT OVERWRITE TABLE {input_table}
            SELECT {total_select_joint} FROM
                (SELECT ai.deviceid as deviceId, ai.ts as ts, ai.value as value, ai.energyType as energyType FROM edinet_hourly_consumption ai
                    WHERE
                        ai.ts >= UNIX_TIMESTAMP("{ts_from}","yyyy-MM-dd HH:mm:ss") AND
                        ai.ts <= UNIX_TIMESTAMP("{ts_to}","yyyy-MM-dd HH:mm:ss") AND
                        ai.deviceid IN ({devices})) a
                JOIN {weather_stations} b on a.deviceId==b.deviceId
                JOIN {solar_stations} b1 on a.deviceId==b1.deviceId
                JOIN  edinet_meteo c on b.stationId==c.stationId and SUBSTR(FROM_UNIXTIME(a.ts), 1, 13) == SUBSTR(FROM_UNIXTIME(c.ts), 1, 13)
                JOIN  edinet_meteo d on b1.stationId==d.stationId and SUBSTR(FROM_UNIXTIME(a.ts), 1, 13) == SUBSTR(FROM_UNIXTIME(d.ts), 1, 13)

                """.format(input_table=input_table,
                           total_select_joint=total_select_joint,
                           ts_from=ts_from,
                           ts_to=ts_to,
                           weather_stations=weather_stations,
                           solar_stations=solar_stations,
                           devices=", ".join("\"{}\"".format(x)
                                             for x in list(device_key.keys())))

        self.logger.debug(sentence)
        qbr.execute_query(sentence)

        #####################################################################################################################################################################################
        """  LOAD from MONGO to HBASE  """
        ######################################################################################################################################################################################
        self.logger.info('Getting')
        try:
            # Launch MapReduce job
            ## Buffered measures to HBase
            self.logger.debug('Baseline Calculation')
            self.launcher_hadoop_job(location, device_key, result_companyId,
                                     save_data_debug)
        except Exception as e:
            raise Exception('MRJob ALIGN process job has failed: {}'.format(e))
        self.logger.info('Module EDINET_baseline execution finished...')
Esempio n. 25
0
def find_optimum_insolation_yearly(longitude, latitude, continuous,
                                   surface_albedo):

    tf = TimezoneFinder()

    fig2, ax2 = plt.subplots(3, sharex=True, figsize=(10, 12))

    legend_labels = []

    sun = ephem.Sun()
    place = ephem.Observer()
    place.lon, place.lat = str(longitude), str(latitude)

    sun_insol = []
    time_series = []
    solar_time_series = []
    daily_irradiation = []
    sun_insol_atmosphere_perDay = []

    beta_logger = []
    sd_logger = []
    minimum_yearly_logger = []

    beta_logger_atm = []
    sd_logger_atm = []
    minimum_yearly_logger_atm = []

    year = 2013
    time_zone = tf.timezone_at(lng=longitude, lat=latitude)
    utc_offset = pendulum.from_timestamp(0, time_zone).offset_hours

    beta_range = range(25, int(latitude + 10))

    for b in beta_range:
        if continuous:
            time = pendulum.datetime(year, 1, 1, 0, 0, 0, tz=time_zone)
            place.date = time.in_tz(
                'UTC'
            )  #place.date.datetime() - datetime.timedelta(hours=utc_offset)

            for i in range(1, 365):
                time_hours, st, daily_irradiation_value, null, null, daily_irradiation_atmosphere = daily_insolation(
                    time, place, utc_offset, sun, b, surface_albedo)
                daily_irradiation.append(daily_irradiation_value)
                sun_insol_atmosphere_perDay.append(
                    daily_irradiation_atmosphere)

                time = time.add(days=1)
                place.date = time.in_tz('UTC')
                progressBar(i, 365, bar_length=20)
                # place.date += 1

            total_yearly_irradiation = numpy.mean(daily_irradiation)
            yearly_deviation = numpy.std(daily_irradiation)
            minimum_yearly = numpy.amin(daily_irradiation)

            smoothed_atm = signal.savgol_filter(sun_insol_atmosphere_perDay,
                                                101, 3)
            total_yearly_irradiation_atm = numpy.mean(smoothed_atm)
            yearly_deviation_atm = numpy.std(smoothed_atm)
            minimum_yearly_atm = numpy.amin(smoothed_atm)

            daily_irradiation = []
            sun_insol_atmosphere_perDay = []

            print(b, ' degrees: ', total_yearly_irradiation, " sd = ",
                  yearly_deviation, "min = ", minimum_yearly)
            print(b, ' degrees: ', total_yearly_irradiation_atm, " sd = ",
                  yearly_deviation_atm, "min = ", minimum_yearly_atm)
            print("")

            beta_logger.append(total_yearly_irradiation)
            sd_logger.append(yearly_deviation)
            minimum_yearly_logger.append(minimum_yearly)

            beta_logger_atm.append(total_yearly_irradiation_atm)
            sd_logger_atm.append(yearly_deviation_atm)
            minimum_yearly_logger_atm.append(minimum_yearly_atm)
        progressBar(b, len(beta_range), bar_length=20)

    ax2[0].plot(beta_range, beta_logger, 'r')
    ax2[0].plot(beta_range, beta_logger_atm, 'r--')
    ax2[0].set_ylabel("Irradiation per year [kJ/m^2/yr]")

    ax2[1].plot(beta_range, sd_logger, 'g')
    ax2[1].plot(beta_range, sd_logger_atm, 'g--')
    ax2[1].set_ylabel("Standard Deviation [kJ/m^2/yr]")

    ax2[2].plot(beta_range, minimum_yearly_logger, 'b')
    ax2[2].plot(beta_range, minimum_yearly_logger_atm, 'b--')
    ax2[2].set_ylabel("Min. irradiation per year [kJ/m^2/yr]")
    ax2[2].set_xlabel("Beta angle [deg]")

    plt.show()
Esempio n. 26
0
def parse(xml):
    parsed_start_datetime = None
    total_distance = {'value': 0.}
    tf = TimezoneFinder()

    def __filter(n):
        cur_p = parsed_points[n]
        last_p = parsed_points[n - 1]
        cheap_distance = 0
        duration = 0.

        if parsed_start_datetime is not None and cur_p['time'] is not None:
            duration = (parse_time(cur_p['time']) -
                        parsed_start_datetime).total_seconds()

        cur_p['duration'] = duration

        # p['smoothed_elevation'] = smoothed_elevations[n]
        speed = smoothed_speeds[n] * 3600. / 1000.
        cur_p['speed'] = speed if duration < 60 else 0.

        if n < 1:
            cur_p['total_distance'] = 0.
        else:
            cheap_distance = cheap_ruler_distance([last_p, cur_p])
            total_distance['value'] += cheap_distance / 1000.
            cur_p['total_distance'] = total_distance['value']

        cur_p['distance'] = cheap_distance
        cur_p['slope'] = 100 * (
            smoothed_elevations[n - 1] - smoothed_elevations[n]
        ) / cheap_distance if cheap_distance > 0 else 0.

        return cur_p

    # Remove namespace to ease nodes selection
    gpx_xml = re.sub(' xmlns="[^"]+"', '', xml, count=1)

    # find namespace for http://www.garmin.com/xmlschemas/TrackPointExtension/v1
    my_namespaces = dict([
        node
        for _, node in ET.iterparse(StringIO(gpx_xml), events=['start-ns'])
    ])
    try:
        track_point_extension_ns_prefix = list(my_namespaces.keys())[list(
            my_namespaces.values()).index(
                'http://www.garmin.com/xmlschemas/TrackPointExtension/v1')]
    except ValueError:
        track_point_extension_ns_prefix = None

    root = ET.fromstring(gpx_xml)

    if root.tag != 'gpx' and root.attrib['version'] != '1.1':
        print('Not a GPX 1.1')

    name = root.find('metadata/name') or root.find('trk/name')
    name = name.text if name is not None else None

    description = root.find('metadata/desc') or root.find('trk/desc')
    description = description.text if description is not None else ''

    parsed_tracks = []
    tracks = root.findall('trk')
    for track in tracks:
        parsed_points = []
        segments = track.findall('trkseg')
        for segment in segments:
            points = segment.findall('trkpt')
            for point in points:
                elevation = point.find('ele')
                time = point.find('time')
                temperature = None
                heart_rate = None
                cadence = None

                if track_point_extension_ns_prefix is not None:
                    extensions = point.find(
                        'extensions/{}:TrackPointExtension'.format(
                            track_point_extension_ns_prefix), my_namespaces)

                    if extensions is not None:
                        temperature = extensions.find(
                            '{}:atemp'.format(track_point_extension_ns_prefix),
                            my_namespaces)
                        heart_rate = extensions.find(
                            '{}:hr'.format(track_point_extension_ns_prefix),
                            my_namespaces)
                        cadence = extensions.find(
                            '{}:cad'.format(track_point_extension_ns_prefix),
                            my_namespaces)

                current_point = {
                    'latitude':
                    float(point.attrib['lat']),
                    'longitude':
                    float(point.attrib['lon']),
                    'elevation':
                    float(elevation.text) if elevation is not None else 0.,
                    'time':
                    time.text if time is not None else None,
                    'temperature':
                    float(temperature.text) if temperature is not None else 0.,
                    'heart_rate':
                    float(heart_rate.text) if heart_rate is not None else 0.,
                    'cadence':
                    float(cadence.text) if cadence is not None else 0.,
                }

                parsed_points.append(current_point)

        track_name = track.find('name')
        track_description = track.find('desc')

        smoothed_speeds = get_smoothed_speed(parsed_points)
        smoothed_elevations = get_smoothed_data(parsed_points, 'elevation')
        uphill, downhill = get_uphill_downhill(smoothed_elevations)

        start_datetime = parsed_points[0]['time']
        end_datetime = parsed_points[-1]['time']
        timezone = tf.timezone_at(lng=parsed_points[0]['longitude'],
                                  lat=parsed_points[0]['latitude'])

        total_time = None
        average_speed = None

        total_distance['value'] = 0.
        parsed_points = list(map(__filter, range(len(parsed_points))))

        moving_time, moving_distance = get_moving_data(parsed_points)
        average_moving_speed = (moving_distance / moving_time
                                ) * 3600. / 1000. if moving_time > 0 else 0.
        distance = parsed_points[-1]['total_distance']

        if start_datetime and end_datetime:
            parsed_start_datetime = parse_time(start_datetime)
            parsed_end_datetime = parse_time(end_datetime)
            total_time = math.fabs(
                (parsed_end_datetime - parsed_start_datetime).total_seconds())
            average_speed = (distance / total_time) * 3600. / 1000.

        parsed_tracks.append({
            'name':
            track_name.text if track_name else None,
            'description':
            track_description.text if track_description else None,
            'start_datetime':
            start_datetime,
            'end_datetime':
            end_datetime,
            'timezone':
            timezone,
            'location':
            get_location(parsed_points[0]),
            'distance':
            distance,
            'moving_distance':
            moving_distance / 1000.,
            'uphill':
            uphill,
            'downhill':
            downhill,
            'min_altitude':
            min(smoothed_elevations),
            'max_altitude':
            max(smoothed_elevations),
            'max_speed':
            max(smoothed_speeds) * 3600. / 1000.,
            'total_time':
            total_time,
            'moving_time':
            moving_time,
            'average_speed':
            average_speed,
            'average_moving_speed':
            average_moving_speed,
            'points':
            parsed_points,
        })

    return name, description, parsed_tracks
Esempio n. 27
0
import pytz
import datetime
import argparse
from timezonefinder import TimezoneFinder
from pygeocoder import Geocoder

location = str(input("Enter location:"))

if location != "":
    # Obtain longitude, latitude via Geocoder
    result = Geocoder.geocode(location)
    coordinate = result[0].coordinates
    location = ", ".join([result[0].city, result[0].country])
    # Fetch Timezone by longitude, latitude via timezonefinder
    tf = TimezoneFinder()
    timezone = tf.timezone_at(lat=coordinate[0], lng=coordinate[1])
	time = datetime.datetime.now(pytz.timezone(timezone))
	print("%s %s" % (location, str(time).split('.')[0]))
Esempio n. 28
0
class Parameters(CleepModule):
    """
    Parameters application.

    Allow to configure Cleep parameters:

        * system time(current time, sunset, sunrise) according to position
        * system locale

    Useful doc:

        * debian timezone: https://wiki.debian.org/TimeZoneChanges
        * python datetime handling: https://hackernoon.com/avoid-a-bad-date-and-have-a-good-time-423792186f30
    """

    MODULE_AUTHOR = "Cleep"
    MODULE_VERSION = "2.1.1"
    MODULE_CATEGORY = "APPLICATION"
    MODULE_DEPS = []
    MODULE_DESCRIPTION = "Configure generic parameters of your device"
    MODULE_LONGDESCRIPTION = (
        "Application that helps you to configure generic parameters of your device"
    )
    MODULE_TAGS = ["configuration", "date", "time", "locale", "lang"]
    MODULE_COUNTRY = None
    MODULE_URLINFO = "https://github.com/tangb/cleepmod-parameters"
    MODULE_URLHELP = None
    MODULE_URLBUGS = "https://github.com/tangb/cleepmod-parameters/issues"
    MODULE_URLSITE = None

    MODULE_CONFIG_FILE = "parameters.conf"
    # default position to raspberry pi foundation
    DEFAULT_CONFIG = {
        "position": {
            "latitude": 52.2040,
            "longitude": 0.1208
        },
        "country": {
            "country": "United Kingdom",
            "alpha2": "GB"
        },
        "timezone": "Europe/London",
        "timestamp": 0,
    }

    SYSTEM_ZONEINFO_DIR = "/usr/share/zoneinfo/"
    SYSTEM_LOCALTIME = "/etc/localtime"
    SYSTEM_TIMEZONE = "/etc/timezone"
    NTP_SYNC_INTERVAL = 60

    def __init__(self, bootstrap, debug_enabled):
        """
        Constructor

        Args:
            bootstrap (dict): bootstrap objects
            debug_enabled (bool): flag to set debug level to logger
        """
        # init
        CleepModule.__init__(self, bootstrap, debug_enabled)

        # members
        self.hostname = Hostname(self.cleep_filesystem)
        self.sun = Sun()
        self.sunset = None
        self.sunrise = None
        self.suns = {
            "sunset": 0,
            "sunset_iso": "",
            "sunrise": 0,
            "sunrise_iso": ""
        }
        self.timezonefinder = TimezoneFinder()
        self.timezone_name = None
        self.timezone = None
        self.time_task = None
        self.sync_time_task = None
        self.__clock_uuid = None
        # code from https://stackoverflow.com/a/106223
        self.__hostname_pattern = (
            r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*"
            r"([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")

        # events
        self.time_now_event = self._get_event("parameters.time.now")
        self.time_sunrise_event = self._get_event("parameters.time.sunrise")
        self.time_sunset_event = self._get_event("parameters.time.sunset")
        self.hostname_update_event = self._get_event(
            "parameters.hostname.update")
        self.country_update_event = self._get_event(
            "parameters.country.update")

    def _configure(self):
        """
        Configure module
        """
        # add clock device if not already added
        if self._get_device_count() < 1:
            self.logger.debug("Add default devices")
            clock = {"type": "clock", "name": "Clock"}
            self._add_device(clock)

        # prepare country
        country = self._get_config_field("country")
        if not country:
            self.set_country()

        # prepare timezone
        timezone_name = self._get_config_field("timezone")
        self.timezone = timezone(timezone_name or get_localzone().zone)

        # compute sun times
        self.set_sun()

        # store device uuids for events
        devices = self.get_module_devices()
        for uuid in devices:
            if devices[uuid]["type"] == "clock":
                self.__clock_uuid = uuid

    def _on_start(self):
        """
        Module starts
        """
        # restore last saved timestamp if system time seems very old (NTP error)
        saved_timestamp = self._get_config_field("timestamp")
        if (int(time.time()) - saved_timestamp) < 0:
            # it seems NTP sync failed, launch timer to regularly try to sync device time
            self.logger.info(
                "Device time seems to be invalid (%s), launch synchronization time task",
                datetime.datetime.utcnow().isoformat(),
            )
            self.sync_time_task = Task(Parameters.NTP_SYNC_INTERVAL,
                                       self._sync_time_task, self.logger)
            self.sync_time_task.start()

        # launch time task (synced to current seconds)
        seconds = 60 - (int(time.time()) % 60)
        self.time_task = Task(60.0, self._time_task, self.logger)
        timer = Timer(0 if seconds == 60 else seconds, self.time_task.start)
        timer.start()

    def _on_stop(self):
        """
        Module stops
        """
        if self.time_task:
            self.time_task.stop()

    def get_module_config(self):
        """
        Get full module configuration

        Returns:
            dict: module configuration
        """
        config = {}

        config["hostname"] = self.get_hostname()
        config["position"] = self.get_position()
        config["sun"] = self.get_sun()
        config["country"] = self.get_country()
        config["timezone"] = self.get_timezone()

        return config

    def get_module_devices(self):
        """
        Return clock as parameters device

        Returns:
            dict: module devices
        """
        devices = super().get_module_devices()

        for uuid in devices:
            if devices[uuid]["type"] == "clock":
                data = self.__format_time()
                data.update({
                    "sunrise": self.suns["sunrise"],
                    "sunset": self.suns["sunset"]
                })
                devices[uuid].update(data)

        return devices

    def __format_time(self):
        """
        Return time with different splitted infos

        Returns:
            dict: time data::

                {
                    timestamp (int): current timestamp
                    iso (string): current datetime in iso 8601 format
                    year (int)
                    month (int)
                    day (int)
                    hour (int)
                    minute (int)
                    weekday (int): 0=monday, 1=tuesday... 6=sunday
                    weekday_literal (string): english literal weekday value (monday, tuesday, ...)
                }

        """
        # current time
        utc_now = utc.localize(datetime.datetime.utcnow())
        local_now = utc_now.astimezone(self.timezone)
        weekday = local_now.weekday()
        if weekday == 0:
            weekday_literal = "monday"
        elif weekday == 1:
            weekday_literal = "tuesday"
        elif weekday == 2:
            weekday_literal = "wednesday"
        elif weekday == 3:
            weekday_literal = "thursday"
        elif weekday == 4:
            weekday_literal = "friday"
        elif weekday == 5:
            weekday_literal = "saturday"
        elif weekday == 6:
            weekday_literal = "sunday"

        return {
            "timestamp": datetime.datetime.timestamp(utc_now),
            "iso": local_now.isoformat(),
            "year": local_now.year,
            "month": local_now.month,
            "day": local_now.day,
            "hour": local_now.hour,
            "minute": local_now.minute,
            "weekday": weekday,
            "weekday_literal": weekday_literal,
        }

    def _sync_time_task(self):
        """
        Sync time task. It is used to try to sync device time using NTP server.

        Note:
            This task is launched only if device time is insane.
        """
        if Parameters.sync_time():
            self.logger.info("Time synchronized with NTP server (%s)" %
                             datetime.datetime.utcnow().isoformat())
            self.sync_time_task.stop()
            self.sync_time_task = None

    def _time_task(self):
        """
        Time task used to refresh time
        """
        now_formatted = self.__format_time()
        self.logger.trace("now_formatted: %s" % now_formatted)

        # send now event
        now_event_params = copy.deepcopy(now_formatted)
        now_event_params.update({
            "sunrise": self.suns["sunrise"],
            "sunset": self.suns["sunset"]
        })
        self.time_now_event.send(params=now_event_params,
                                 device_id=self.__clock_uuid)

        # send sunrise event
        if self.sunrise:
            if (now_formatted["hour"] == self.sunrise.hour
                    and now_formatted["minute"] == self.sunrise.minute):
                self.time_sunrise_event.send(device_id=self.__clock_uuid)

        # send sunset event
        if self.sunset:
            if (now_formatted["hour"] == self.sunset.hour
                    and now_formatted["minute"] == self.sunset.minute):
                self.time_sunset_event.send(device_id=self.__clock_uuid)

        # update sun times after midnight
        if now_formatted["hour"] == 0 and now_formatted["minute"] == 5:
            self.set_sun()

        # save last timestamp in config to restore it after a reboot and NTP sync failed (no internet)
        if not self.sync_time_task:
            self._set_config_field("timestamp", now_formatted["timestamp"])

    def set_hostname(self, hostname):
        """
        Set raspi hostname

        Args:
            hostname (string): hostname

        Returns:
            bool: True if hostname saved successfully, False otherwise

        Raises:
            InvalidParameter: if hostname has invalid format
        """
        # check hostname
        if re.match(self.__hostname_pattern, hostname) is None:
            raise InvalidParameter("Hostname is not valid")

        # update hostname
        res = self.hostname.set_hostname(hostname)

        # send event to update hostname on all devices
        if res:
            self.hostname_update_event.send(params={"hostname": hostname})

        return res

    def get_hostname(self):
        """
        Return raspi hostname

        Returns:
            string: raspberry pi hostname
        """
        return self.hostname.get_hostname()

    def set_position(self, latitude, longitude):
        """
        Set device position

        Args:
            latitude (float): latitude
            longitude (float): longitude

        Raises:
            CommandError: if error occured during position saving
        """
        if latitude is None:
            raise MissingParameter('Parameter "latitude" is missing')
        if not isinstance(latitude, float):
            raise InvalidParameter('Parameter "latitude" is invalid')
        if longitude is None:
            raise MissingParameter('Parameter "longitude" is missing')
        if not isinstance(longitude, float):
            raise InvalidParameter('Parameter "longitude" is invalid')

        # save new position
        position = {"latitude": latitude, "longitude": longitude}

        if not self._set_config_field("position", position):
            raise CommandError("Unable to save position")

        # reset python time to take into account last modifications before
        # computing new times
        time.tzset()

        # and update related stuff
        self.set_timezone()
        self.set_country()
        self.set_sun()

        # send now event
        self._time_task()

    def get_position(self):
        """
        Return device position

        Returns:
            dict: position coordinates::

                {
                    latitude (float),
                    longitude (float)
                }

        """
        return self._get_config_field("position")

    def get_sun(self):
        """
        Compute sun times

        Returns:
            dict: sunset/sunrise timestamps::

                {
                    sunrise (int),
                    sunset (int)
                }

        """
        return self.suns

    def set_sun(self):
        """ "
        Compute sun times (sunrise and sunset) according to configured position
        """
        # get position
        position = self._get_config_field("position")

        # compute sun times
        self.sunset = None
        self.sunrise = None
        if position["latitude"] != 0 and position["longitude"] != 0:
            self.sun.set_position(position["latitude"], position["longitude"])
            self.sunset = self.sun.sunset().astimezone(self.timezone)
            self.sunrise = self.sun.sunrise().astimezone(self.timezone)
            self.logger.debug("Found sunrise:%s sunset:%s" %
                              (self.sunrise, self.sunset))

            # save times
            self.suns["sunrise"] = int(self.sunrise.strftime("%s"))
            self.suns["sunrise_iso"] = self.sunrise.isoformat()
            self.suns["sunset"] = int(self.sunset.strftime("%s"))
            self.suns["sunset_iso"] = self.sunset.isoformat()

    def set_country(self):
        """
        Compute country (and associated alpha) from current internal position

        Warning:
            This function can take some time to find country info on slow device like raspi 1st generation (~15secs)
        """
        # get position
        position = self._get_config_field("position")
        if not position["latitude"] and not position["longitude"]:
            self.logger.debug(
                "Unable to set country from unspecified position (%s)" %
                position)
            return

        # get country from position
        country = {"country": None, "alpha2": None}
        try:
            # search country
            coordinates = ((position["latitude"], position["longitude"]), )
            # need a tuple
            geo = reverse_geocode.search(coordinates)
            self.logger.debug("Found country infos from position %s: %s" %
                              (position, geo))
            if (geo and len(geo) > 0 and "country_code" in geo[0]
                    and "country" in geo[0]):
                country["alpha2"] = geo[0]["country_code"]
                country["country"] = geo[0]["country"]

            # save new country
            if not self._set_config_field("country", country):
                raise CommandError("Unable to save country")

            # send event
            self.country_update_event.send(params=country)

        except CommandError:
            raise

        except Exception:
            self.logger.exception("Unable to find country for position %s:" %
                                  position)

    def get_country(self):
        """
        Get country from position

        Returns:
            dict: return country infos::

            {
                country (string): country label
                alpha2 (string): country code
            }

        """
        return self._get_config_field("country")

    def set_timezone(self):
        """
        Set timezone according to coordinates

        Returns:
            bool: True if function succeed, False otherwise

        Raises:
            CommandError: if unable to save timezone
        """
        # get position
        position = self._get_config_field("position")
        if not position["latitude"] and not position["longitude"]:
            self.logger.warning(
                "Unable to set timezone from unspecified position (%s)" %
                position)
            return False

        # compute timezone
        current_timezone = None
        try:
            # try to find timezone at position
            current_timezone = self.timezonefinder.timezone_at(
                lat=position["latitude"], lng=position["longitude"])
            if current_timezone is None:
                # extend search to closest position
                # TODO increase delta_degree to extend research, careful it use more CPU !
                current_timezone = self.timezonefinder.closest_timezone_at(
                    lat=position["latitude"], lng=position["longitude"])
        except ValueError:
            # the coordinates were out of bounds
            self.logger.exception("Coordinates out of bounds")
        except Exception:
            self.logger.exception(
                "Error occured searching timezone at position")
        if not current_timezone:
            self.logger.warning(
                "Unable to set device timezone because it was not found")
            return False

        # save timezone value
        self.logger.debug("Save new timezone: %s" % current_timezone)
        if not self._set_config_field("timezone", current_timezone):
            raise CommandError("Unable to save timezone")

        # configure system timezone
        zoneinfo = os.path.join(self.SYSTEM_ZONEINFO_DIR, current_timezone)
        self.logger.debug("Checking zoneinfo file: %s" % zoneinfo)
        if not os.path.exists(zoneinfo):
            raise CommandError('No system file found for "%s" timezone' %
                               current_timezone)
        self.logger.debug('zoneinfo file "%s" exists' % zoneinfo)
        self.cleep_filesystem.rm(self.SYSTEM_LOCALTIME)

        self.logger.debug('Writing timezone "%s" in "%s"' %
                          (current_timezone, self.SYSTEM_TIMEZONE))
        if not self.cleep_filesystem.write_data(self.SYSTEM_TIMEZONE,
                                                "%s" % current_timezone):
            self.logger.error(
                'Unable to write timezone data on "%s". System timezone is not configured!'
                % self.SYSTEM_TIMEZONE)
            return False

        # launch timezone update in background
        self.logger.debug("Updating system timezone")
        command = Console()
        res = command.command(
            "/usr/sbin/dpkg-reconfigure -f noninteractive tzdata",
            timeout=15.0)
        self.logger.debug("Timezone update command result: %s" % res)
        if res["returncode"] != 0:
            self.logger.error("Error reconfiguring system timezone: %s" %
                              res["stderr"])
            return False

        # TODO configure all wpa_supplicant.conf country code

        # propagate changes to cleep
        self.timezone = timezone(current_timezone)
        self._time_task()

        return True

    def get_timezone(self):
        """
        Return timezone

        Returns:
            string: current timezone name
        """
        return self._get_config_field("timezone")

    @staticmethod
    def sync_time():
        """
        Synchronize device time using NTP server

        Note:
            This command may lasts some seconds

        Returns:
            bool: True if NTP sync succeed, False otherwise
        """
        console = Console()
        resp = console.command("/usr/sbin/ntpdate-debian", timeout=60.0)

        return resp["returncode"] == 0

    def get_non_working_days(self, year=None):
        """
        Return non working days of current year

        Args:
            year (int): get non working day for specified year. If not specified use current year

        Returns:
            list: list of non working days of the year. List can be empty if error occured::

            [
                {
                    datetime (string): non working day (date in iso format YYYY-MM-DD)
                    label (string): english name of non working day
                },
                ...
            ]

        """
        self._check_parameters([{
            "name": "year",
            "type": int,
            "value": year,
            "none": True,
        }])

        try:
            country = self._get_config_field("country")
            continent_code = country_alpha2_to_continent_code(
                country["alpha2"])
            continent_name = convert_continent_code_to_continent_name(
                continent_code).lower()
            continent_name = (continent_name.lower().replace(
                "south", "").replace("north", "").strip())

            workalendar = importlib.import_module(
                f"workalendar.{continent_name}")
            fixed_country = "".join(
                [part.capitalize() for part in country["country"].split()])
            _class = getattr(workalendar, fixed_country)
            _instance = _class()
            year = year or datetime.datetime.now().year
            holidays = _instance.holidays(year)
            return [(date.isoformat(), label) for (date, label) in holidays]
        except Exception:
            self.logger.exception("Unable to get non working days:")
            return []

    def is_non_working_day(self, day):
        """
        Check if specified day is non working day according to current locale configuration

        Args:
            day (string): day to check (must be iso format XXXX-MM-DD)

        Returns:
            bool: True if specified day is a non working day, False otherwise
        """
        self._check_parameters([{
            "name": "day",
            "type": str,
            "value": day,
        }])

        year = datetime.date.fromisoformat(day).year
        non_working_days = self.get_non_working_days(year=year)
        return any(a_day == day for (a_day, label) in non_working_days)

    def is_today_non_working_day(self):
        """
        Check if today is non working day according to current locale configuration

        Args:
            day (datetime.date): day to check

        Returns:
            bool: True if specified day is a non working day, False otherwise
        """
        today = datetime.date.today()
        return self.is_non_working_day(today.isoformat())
Esempio n. 29
0
 # Coordinates
 gx_track = d.getElementsByTagName('gx:Track')[0]
 gx_coord_element = gx_track.getElementsByTagName('gx:coord')
 if gx_coord_element.length > 0:
     gx_coord = gx_track.getElementsByTagName('gx:coord')[0]
     coords = gx_coord.firstChild.data.split(' ')
     longitude = float(coords[0])
     latitude = float(coords[1])
 else:
     break
 #print("longitude:{0}, latitude:{1}".format(longitude, latitude))
 # Get timezone of the coordinates
 # t = datetime.tzinfo("America/New_York")
 tf = TimezoneFinder()
 tz = tzwhere.tzwhere(shapely=True)
 timezone_str = tf.timezone_at(lng=longitude, lat=latitude)
 # timezone_str = tzwhere.tzNameAt(37.3880961, -5.9823299)
 #print("timezone of current location: {0}".format(timezone_str))
 t = pytz.timezone(timezone_str)
 # begin and end time of the visiting
 timespan = d.getElementsByTagName('TimeSpan')[0]
 begin = timespan.getElementsByTagName('begin')[0].firstChild.data
 begin_datetime = dateutil.parser.parse(begin).astimezone(t)
 end = timespan.getElementsByTagName('end')[0].firstChild.data
 end_datetime = dateutil.parser.parse(end).astimezone(t)
 begin_date = begin_datetime.date()
 begin_time = begin_datetime.time()
 end_date = end_datetime.date()
 end_time = end_datetime.time()
 #print("Begin date: {0}, Begin time: {1}, End date: {2}, End time: {3}".format(begin_date, begin_time,end_date, end_time))
 # insert sql row using this data
Esempio n. 30
0
def plot_insolation_yearly(beta, longitude, latitude, continuous,
                           surface_albedo):

    line_colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k']

    tf = TimezoneFinder()

    fig2, ax2 = plt.subplots(1, figsize=(10, 8))

    legend_labels = []

    sun = ephem.Sun()
    place = ephem.Observer()
    place.lon, place.lat = str(longitude), str(latitude)

    sun_insol = []
    time_series = []
    solar_time_series = []
    daily_irradiation = []
    sun_insol_atmosphere_perDay = []

    year = 2013
    time_zone = tf.timezone_at(lng=longitude, lat=latitude)
    utc_offset = pendulum.from_timestamp(0, time_zone).offset_hours

    for count, b in enumerate(beta):
        if continuous:
            start_time = pendulum.datetime(year, 1, 1, 0, 0, 0, tz=time_zone)
            for i in range(1, 365):
                time_hours, st, daily_irradiation_value, null, null, daily_irradiation_atmosphere = daily_insolation(
                    start_time, place, utc_offset, sun, b, surface_albedo)
                daily_irradiation.append(daily_irradiation_value)
                sun_insol_atmosphere_perDay.append(
                    daily_irradiation_atmosphere)
                start_time = start_time.add(days=1)
                progressBar(i, 365, bar_length=20)

                # if (i<300 and i >293):
                #     time_hours, st, daily_irradiation_value ,null,null,daily_irradiation_atmosphere = daily_insolation(start_time,place,utc_offset,sun,b,surface_albedo)
                #     print(i, daily_irradiation_atmosphere)

            savgol_smoothed = signal.savgol_filter(sun_insol_atmosphere_perDay,
                                                   101, 5)
            ax2.plot(range(1, 365),
                     daily_irradiation,
                     c=line_colors[count],
                     label='Angle: {0}'.format(b))
            ax2.plot(range(1, 365),
                     sun_insol_atmosphere_perDay,
                     ':',
                     c=line_colors[count])
            ax2.plot(range(1, 365),
                     savgol_smoothed,
                     '--',
                     c=line_colors[count])
            print("")
            print(b, ' degrees (extra total): ',
                  format_e(integrate.simps(daily_irradiation)), "kJ m-2")
            print(b, ' degrees (atm raw total): ',
                  format_e(integrate.simps(sun_insol_atmosphere_perDay)),
                  "kJ m-2")
            print(b, ' degrees (atm savgol total): ',
                  format_e(integrate.simps(savgol_smoothed)), "kJ m-2")
            # current_label = 'Angle: {0}'.format(b)
            # legend_labels.append(current_label)

            daily_irradiation = []
            sun_insol_atmosphere_perDay = []

        if continuous:
            ax2.legend()  #(labels = legend_labels,loc='upper right')
            ax2.set_xlabel("Day Number")
            ax2.set_ylabel("Irradiation [kJ/m^2]")
        #plt.savefig('graph of sun position per month.png', dpi = 600)
    plt.show()
Esempio n. 31
0
def check_timezone_gps(locations):
    tf = TimezoneFinder(in_memory=True)
    TimezoneFinder.using_numba()
    return tf.timezone_at(lng=locations.longitude, lat=locations.latitude)
Esempio n. 32
0
def plot_insolation_hourly(beta, longitude, latitude, surface_albedo):
    lines = ["-", "--", "-.", ":"]
    linecycler = cycle(lines)
    month_list = [
        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
        'Nov', 'Dec'
    ]

    tf = TimezoneFinder()

    fig2, ax2 = plt.subplots(1, figsize=(10, 8))

    legend_labels = []

    sun = ephem.Sun()
    place = ephem.Observer()
    place.lon, place.lat = str(longitude), str(latitude)

    sun_insol = []
    time_series = []
    solar_time_series = []
    daily_irradiation = []

    year = 2013
    day = 29

    time_zone = tf.timezone_at(lng=longitude, lat=latitude)

    utc_offset = pendulum.from_timestamp(0, time_zone).offset_hours

    for b in beta:
        for month in range(6, 7):
            time = pendulum.datetime(year, month, day, 0, 0, 0, tz=time_zone)
            place.date = time.in_tz('UTC')
            time_hours, st, daily_irradiation_value, sun_insol, sun_insol_atmosphere, daily_irradiation_atmosphere = daily_insolation(
                time, place, utc_offset, sun, b, surface_albedo)

            top = max(sun_insol)
            sun_insol = [x / top for x in sun_insol]

            ax2.plot(time_hours, sun_insol, next(linecycler))

            # ax2.plot(time_hours,R_b_collector[0],next(linecycler))
            # ax2.plot(time_hours,R_b_collector[1],next(linecycler))

            # time = pendulum.datetime(year,month,3,0,0,0,tz=time_zone)
            # place.date = time.in_tz('UTC')
            # time_hours, st, daily_irradiation_value ,sun_insol,sun_insol_atmosphere,daily_irradiation_atmosphere = daily_insolation(time,place,utc_offset,sun,b,surface_albedo)
            # ax2.plot(time_hours,sun_insol,next(linecycler))

            current_label = 'Month: {1}, Angle: {0}'.format(
                b, month_list[month - 1])
            legend_labels.append(current_label)

    top = max(sun_insol)
    # minor_ticks = numpy.arange(0, 25, 24)
    # ax2.set_xticks(minor_ticks, minor=True)
    ax2.grid(which='both')
    ax2.legend(labels=legend_labels, loc='upper right')
    ax2.set_xlabel("Time [hrs from midnight]")
    ax2.set_ylabel("Irradiation [kJ/m^2]")
    #plt.savefig('graph of sun position per month.png', dpi = 600)
    plt.show()
Esempio n. 33
0
def timezone_name_from_coordinates(lng: float, lat: float) -> str:
    tf = TimezoneFinder()
    return tf.timezone_at(lng=lng, lat=lat)
Esempio n. 34
0
    def __normalize(self, session_log):
        '''
        Normalize and fill missing fields in a session log
        :param session_log: climbing session object
        :type session_log: dict
        :return: normalized session object
        :rtype: dict
        '''
        try:
            available_fields = session_log.keys()
            location = get_location(session_log['location'])

            if not location:
                raise Exception(
                    f"No location found for {session_log['location']}. Current supported locations include: {get_location_names()}")
            # Split the style field by commas and turn it into a list instead
            #  This is because style could be multiple fields ie. indoor bouldering, indoor lead
            session_log['style'] = session_log['style'].split(',')
            # Compare the counter list with the location's grading scale
            # If any counters are missing, add to it and just defult to 0 for all categories
            counted_grades = []
            for counter in session_log['counter']:
                counted_grades.append(counter['grade'])
            missing_counters = list(
                set(location.grading) - set(counted_grades))
            for counter in missing_counters:
                default_counter = {'grade': counter, 'flash': 0,
                                   'redpoint': 0, 'repeat': 0, 'attempts': 0}
                if location.is_outdoor:
                    default_counter['onsight'] = 0
                session_log['counter'].append(default_counter)
            session_log['counter'] = reformat_counter(
                session_log['counter'])
            # Look at the optional fields and add filler values
            if 'climbers' not in available_fields:
                session_log['climbers'] = None
            if 'injury' not in available_fields:
                session_log['injury'] = {'isTrue': False, 'description': None}
            elif session_log['injury']['isTrue'] == False and session_log['injury']['description'] == 'Add a description of injury here':
                session_log['injury'] = {'isTrue': False, 'description': None}
            if 'media' not in available_fields:
                session_log['media'] = None
            if 'projects' not in available_fields:
                session_log['projects'] = None
            else:
                for climb in session_log['projects']:
                    if 'notes' not in climb.keys():
                        climb['notes'] = None
                    if 'media' not in climb.keys():
                        climb['media'] = None
            session_log['projects'] = reformat_projects(
                session_log['projects'])
            # Initializing Timezone info
            tf = TimezoneFinder()
            location = get_location(session_log['location'])
            location_tz = pytz.timezone(tf.timezone_at(
                lng=location.lon, lat=location.lat))
            # Add time zone to start and end times
            local_start = location_tz.normalize(location_tz.localize(datetime.datetime.combine(
                session_log['date'], common.str_to_time(session_log['time']['start']).time())))
            local_end = location_tz.normalize(location_tz.localize(datetime.datetime.combine(
                session_log['date'], common.str_to_time(session_log['time']['end']).time())))
            # Convert to UTC
            session_log['date'] = datetime.datetime.combine(
                session_log['date'], common.str_to_time(session_log['time']['start']).time())
            session_log['time']['start'] = local_start
            session_log['time']['end'] = local_end
            return session_log
        except Exception as ex:
            raise ex
Esempio n. 35
0
def get_tz(lat: float, lng: float) -> str:
    """ Calculates timezone from coordinates """
    tf = TimezoneFinder()
    tz = tf.timezone_at(lng=lng, lat=lat)
    return tz
Esempio n. 36
0
 def getTimezone(self):
     tf = TimezoneFinder(in_memory=True)
     return tf.timezone_at(lng=self.longitude,
                           lat=self.latitude)  # America/Toronto
Esempio n. 37
0
from timezonefinder import TimezoneFinder

TimezoneFinder.using_numba()  # this is a static method returning True or False

tf = TimezoneFinder()
# or
tf = TimezoneFinder(in_memory=True)

longitude, latitude = 13.358, 52.5061
tf.timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Berlin'
tf.certain_timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Berlin'

longitude = 12.773955
latitude = 55.578595
tf.closest_timezone_at(lng=longitude, lat=latitude)  # returns 'Europe/Copenhagen'

longitude = 42.1052479
latitude = -16.622686
tf.closest_timezone_at(lng=longitude, lat=latitude, delta_degree=2, exact_computation=True, return_distances=True,
                       force_evaluation=True)

tf.get_geometry(tz_name='Africa/Addis_Ababa', coords_as_pairs=True)
tf.get_geometry(tz_id=400, use_id=True)


# To maximize the chances of getting a result in a Django view it might look like:

def find_timezone(request, lat, lng):
    lat = float(lat)
    lng = float(lng)