Get time from city name using Python - python

As you can see in the title, I want to find the time of given city in Python. How can I achieve this? I've tried geopy and timezonefinder modules but they are giving me different results too. (like 'What time is it in Spotify?', 'It's 12:04')
What I'm trying to achieve is:
What time is it in California?
It's 16:15
THE CODE
import nltk
import datetime
import calendar
import pytz
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
self.inp = input("City name: ")
# Find city name using NLP
# Get city name
findCityName = str(self.inp.title())
# NLP
word = nltk.word_tokenize(findCityName)
pos_tag = nltk.pos_tag(word)
chunk = nltk.ne_chunk(pos_tag)
self.inp = [ " ".join(w for w, t in ele) for ele in chunk if isinstance(ele, nltk.Tree)]
self.inp = ' '.join(self.inp)
# Get lat, long from city name
geolocator = Nominatim(user_agent='xxx')
location = geolocator.geocode(self.inp.capitalize())
# Get timezone from coordinates
tf = TimezoneFinder()
latitude, longitude = location.latitude, location.longitude
# Timezone
datez = tf.timezone_at(lng=longitude, lat=latitude)
datez = str(datez)
globalDate = datetime.datetime.now(pytz.timezone(datez))
print("The date in " + str(self.inp) + " is: " + globalDate.strftime('%A, %m/%d/%y'))

Related

Geocoder in python takes too much time to run

I'm trying to associate the city/country/state name to the latitude and longitude of my dataset. This is how I made it:
import pandas as pd
import io
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
def city_state_country(row):
coord = f"{row['latitude']}, {row['longitude']}"
location = geolocator.reverse(coord, exactly_one=True)
address = location.raw['address']
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
row['city'] = city
row['state'] = state
row['country'] = country
return row
ddf_slice= ddf_slice.apply(city_state_country, axis=1)
but I have so many rows and it takes forever. how can I solve this?

IEEE C37.118 python parser of PMU RAW file

Here is sample output data that comes from a Phasor Measurement Unit (PMU) device placed in the distribution network. PMU device is based on IEEE C37.118 standard. Here are sample 3 records:
+---------+---------------+----------+
15:28:59,420,551 ETHER
|0 |f4|03|43|3e|e0|18|00|45|1d|62|5f|f9|08|00|45|00|00|4e|a7|88|00|00|3c|06|35|d3|c0|a8|84|85|0a|14|52|0d|12|68|ee|2c|08|1d|21|f6|27|9a|9b|fa|50|18|20|00|7e|06|00|00|aa|01|00|26|00|06|60|21|58|bb|ff|06|68|20|08|04|f2|48|d7|b1|00|00|00|00|fe|3c|fb|47|fe|37|fb|46|00|04|ff|ff|f2|db|
+---------+---------------+----------+
15:28:59,440,855 ETHER
|0 |f4|03|43|3e|e0|18|00|45|1d|62|5f|f9|08|00|45|00|00|4e|a7|8c|00|00|3c|06|35|cf|c0|a8|84|85|0a|14|52|0d|12|68|ee|2c|08|1d|22|1c|27|9a|9b|fa|50|18|20|00|a5|d5|00|00|aa|01|00|26|00|06|60|21|58|bb|ff|06|b6|00|08|04|f2|4d|d7|b0|00|00|00|00|fe|3c|fb|47|fe|37|fb|46|00|04|ff|ff|7d|02|
+---------+---------------+----------+
Does anyone has idea where can I find Python implementation, PIP package or anything similar for parser that could convert this data in some sensible dataframe?
Any info could be useful.
I've created script that parses these types of files and outputs them in Excel:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
import pandas as pd
import numpy as np
import datetime
from datetime import timedelta
import pytz
from datetime import datetime
import pytz
from tzlocal import get_localzone
tz = pytz.timezone('Europe/London')
listofrecords = []
i=0
finalDict = {}
with open("inputData/PMU-Location1.txt",'r') as pmuraw:
for line in pmuraw.readlines(): #read in multiple lines
if len(line.strip())==0:
continue
if line.startswith('+---------+---------------+----------+'):
record = {}
if "ETHER" in line:
record["Time"],record["Network"] = line.strip().rstrip().split(sep=" ")
if "|" in line:
for i, value in enumerate(line.strip().rstrip().split(sep="|")):
record[i] = value
record["SYNC"] = int((str(record[56])+" "+str(record[57])).strip().rstrip().replace(" ",""),16)
record["Packet size"] = int((str(record[58])+" "+str(record[59])).strip().rstrip().replace(" ",""),16)
record["PMU ID"] = int((str(record[60])+" "+str(record[61])).strip().rstrip().replace(" ",""),16)
record["Timestamp"] = datetime.fromtimestamp(int((str(record[62])+" "+str(record[63])+" "+str(record[64])+" "+str(record[65])).strip().rstrip().replace(" ",""), 16)).astimezone(tz).strftime('%d.%m.%Y %H:%M:%S')
record["Time diff"] = str(timedelta(microseconds = int((str(record[67])+" "+str(record[68])+""+str(record[69])).strip().rstrip().replace(" ",""),16)))[6:]
record["Status PMU"] = int((str(record[70])+" "+str(record[71])).strip().rstrip().replace(" ",""),16)
record["Fazor 1"] = int((str(record[72])+" "+str(record[73])+" "+ str(record[74])+" "+str(record[75])).strip().rstrip().replace(" ",""),16)
record["Fazor 2"] = int((str(record[76])+" "+str(record[77])+" "+ str(record[78])+" "+str(record[79])).strip().rstrip().replace(" ",""),16)
record["Fazor 3"] = int((str(record[80])+" "+str(record[81])+" "+ str(record[82])+" "+str(record[83])).strip().rstrip().replace(" ",""),16)
record["Fazor 4"] = int((str(record[84])+" "+str(record[85])+" "+ str(record[86])+" "+str(record[87])).strip().rstrip().replace(" ",""),16)
record["Frequency"] = int((str(record[88])+" "+str(record[89])).strip().rstrip().replace(" ",""),16)
record["Delta freq"] = int((str(record[90])+" "+str(record[91])).strip().rstrip().replace(" ",""),16)
remove = [k for k in record if isinstance(k, (int, np.integer))]
for k in remove: del record[k]
listofrecords.append(record)
PMU-Location1DF = pd.DataFrame(listofrecords)
PMU-Location1DF.to_excel("outputData/PMU-Location1.xlsx",sheet_name='PMU Žerjavinec', encoding='UTF-8')

Finding nearby cities using Google API

I want to get nearby cities from passed latitude and longitude. I have used the geonames and geobytes APIs but want to use Google API for finding nearby cities.
This is my code:
def getNearbyCities(self):
# lat, lon = self.getLatLon()
# res_url = urlopen('http://gd.geobytes.com/GetNearbyCities?callback=?&radius=100&limit=100&Latitude=' + str(lat) + '&Longitude=' + str(lon))
res_url = urlopen('http://getnearbycities.geobytes.com/GetNearbyCities?callback=?&radius=100&locationcode=' + str(self.city))
resp = str(res_url.read())
print(resp)
validate_res = resp.split("b'?(")[-1].split(");'")[0]
validated_res = ast.literal_eval(validate_res)
cities_nd_distence = []
for data in validated_res:
data_tuple = (data[1], data[7])
if data[1] not in cities_nd_distence:
cities_nd_distence.append(data_tuple)
import pprint
pprint.pprint(cities_nd_distence)
return cities_nd_distence
If you only want to get cities based on latitude and longitude, you can have a look at https://github.com/coderholic/django-cities
from cities.models import City
from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import Distance
p = Point(-118, 34, srid=4326)
City.objects.annotate(distance=Distance('location', p)).order_by("distance").first()
<City: Hacienda Heights>

Sort images based on age

I'm using the following code to load the IMDB dataset and mat file then isolate them based on the ages. The following code loads the mat file and then it computes the age by subtracting the data in which the photo was taken from the date of birth
from scipy.io import loadmat
from datetime import datetime
import os
import numpy as np
from shutil import copy
db = "imdb"
mat_path = "data/{}_crop/{}.mat".format(db, db)
print (mat_path)
dataset = loadmat(mat_path)
face_score_treshold = 3
path_root = "data/{}_crop/".format(db)
def calc_age(taken, dob):
birth = datetime.fromordinal(max(int(dob) - 366, 1))
# assume the photo was taken in the middle of the year
if birth.month < 7:
return taken - birth.year
else:
return taken - birth.year - 1
image_names_array = dataset['imdb']['full_path'][0, 0][0]
dob = dataset[db][0, 0]["dob"][0] # Matlab serial date number
photo_taken = dataset[db][0, 0]["photo_taken"][0] # year
image_names = []
age = [calc_age(photo_taken[i], dob[i]) for i in range(len(dob))]
print(age)
for image_name_arg in range(image_names_array.shape[0]):
try:
os.makedirs(str (age[image_name_arg]))
except OSError:
pass
copy(path_root + "/" + str(image_names_array[image_name_arg][0]),str (age[image_name_arg]))
image_name = image_names_array[image_name_arg][0]
image_names.append(image_name)
print( dict(zip(image_names, age)))
After calculating the age, it creates the folder with age as the folder name and it copies the image files to the folder.
However there are lots of false negatives in the resulting folder. How can I be able to sort this out?

Python: invalid syntax: <string>, line 1, pos 16

I have developed a code in Python in which -in order to run the program- I need to take some arguments from the command line. But I am getting continuously the same error:
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
invalid syntax: <string>, line 1, pos 16
I have the faintest idea what is wrong with my code. So, I present my code below in case someone could help me:
import QSTK.qstkutil.qsdateutil as du
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkutil.DataAccess as da
import datetime as dt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import time
import math
import copy
import QSTK.qstkstudy.EventProfiler as ep
import csv
import sys
import argparse
def readData(li_startDate, li_endDate, ls_symbols):
#Create datetime objects for Start and End dates (STL)
dt_start = dt.datetime(li_startDate[0], li_startDate[1], li_startDate[2])
dt_end = dt.datetime(li_endDate[0], li_endDate[1], li_endDate[2])
#Initialize daily timestamp: closing prices, so timestamp should be hours=16 (STL)
dt_timeofday = dt.timedelta(hours=16)
#Get a list of trading days between the start and end dates (QSTK)
ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
#Create an object of the QSTK-dataaccess class with Yahoo as the source (QSTK)
c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
#Keys to be read from the data
ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
#Read the data and map it to ls_keys via dict() (i.e. Hash Table structure)
ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))
return [d_data, dt_start, dt_end, dt_timeofday, ldt_timestamps]
def marketsim(cash,orders_file,values_file):
orders = pd.read_csv(orders_file,index_col='Date',parse_dates=True,header=None)
ls_symbols = list(set(orders['X.4'].values))
df_lastrow = len(orders) - 1
dt_start = dt.datetime(orders.get_value(0, 'X.1'),orders.get_value(0, 'X.2'),orders.get_value(0, 'X.3'))
dt_end = dt.datetime(orders.get_value(df_lastrow, 'X.1'),orders.get_value(df_lastrow, 'X.2'),orders.get_value(df_lastrow, 'X.3') + 1 )
#d_data = readData(dt_start,dt_end,ls_symbols)
#Initialize daily timestamp: closing prices, so timestamp should be hours=16 (STL)
dt_timeofday = dt.timedelta(hours=16)
#Get a list of trading days between the start and end dates (QSTK)
ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
#Create an object of the QSTK-dataaccess class with Yahoo as the source (QSTK)
c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)
#Keys to be read from the data
ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
#Read the data and map it to ls_keys via dict() (i.e. Hash Table structure)
df_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))
ls_symbols.append("_CASH")
trades = pd.Dataframe(index=list(ldt_timestamps[0]),columns=list(ls_symbols))
current_cash = cash
trades["_CASH"][ldt_timestamps[0]] = current_cash
current_stocks = dict()
for symb in ls_symbols:
current_stocks[symb] = 0
trades[symb][ldt_timestamps[0]] = 0
for row in orders.iterrows():
row_data = row[1]
current_date = dt.datetime(row_data['X.1'],row_data['X.2'],row_data['X.3'],16)
symb = row_data['X.4']
stock_value = d_data['close'][symb][current_date]
stock_amount = row_data['X.6']
if row_data['X.5'] == "Buy":
current_cash = current_cash - (stock_value*stock_amount)
trades["_CASH"][current_date] = current_cash
current_stocks[symb] = current_stocks[symb] + stock_amount
trades[symb][current_date] = current_stocks[symb]
else:
current_cash = current_cash + (stock_value*stock_amount)
trades["_CASH"][current_date] = current_cash
current_stocks[symb] = current_stocks[symb] - stock_amount
trades[symb][current_date] = current_stocks[symb]
#trades.fillna(method='ffill',inplace=True)
#trades.fillna(method='bfill',inplace=False)
trades.fillna(0)
#alt_cash = current_cash
#alt_cash = trades.cumsum()
value_data = pd.Dataframe(index=list(ldt_timestamps),columns=list("V"))
value_data = value_data.fillna(0)
value_data = value_data.cumsum(axis=0)
for day in ldt_timestamps:
value = 0
for sym in ls_symbols:
if sym == "_CASH":
value = value + trades[sym][day]
else:
value = calue + trades[sym][day]*d_data['close'][sym][day]
value_data["V"][day] = value
fileout = open(values_file,"w")
for row in value_data.iterrows():
file_out.writelines(str(row[0].strftime('%Y,%m,%d')) + ", " + str(row[1]["V"].round()) + "\n" )
fileout.close()
def main(argv):
if len(sys.argv) != 3:
print "Invalid arguments for marketsim.py. It should be of the following syntax: marketsim.py orders_file.csv values_file.csv"
sys.exit(0)
#initial_cash = int (sys.argv[1])
initial_cash = 1000000
ordersFile = str(sys.argv[1])
valuesFile = str(sys.argv[2])
marketsim(initial_cash,ordersFile,valuesFile)
if __name__ == "__main__":
main(sys.argv[1:])
The input I gave to the command line was:
python marketsim.py orders.csv values.csv
I guess that the problem lies either into the imports or probably into the main function(incl. the if below the def main(argv)
I have to point out that the files orders.csv and values.csv exist and are located into the same folder.
I hope have made everything clear.
So, I am looking forward to reading your answers community-mates! :D
Thank you!

Categories