def utcToLocal(latitude, longitude, UTC): utc_dt = utc.localize(datetime.utcfromtimestamp(UTC)) local_tz = timezone(tzwhere.tzNameAt(latitude, longitude)) #print(local_tz) local_time = utc_dt.astimezone(local_tz) return local_time
def create_site(name, address): location = geolocator.geocode(address, addressdetails=True) if type(location) == "NoneType": console.error("Unable to find the address %s" % address) else: console.info("Address found: %s" % location) tz = tzwhere.tzNameAt(location.latitude, location.longitude) country_code = str(location.raw["address"]["country_code"]).upper() response = mist_lib.orgs.sites.create(mist, org_id, name=name, address=location.address, lat=location.latitude, lng=location.longitude, timezone=tz, country_code=country_code) if response["status_code"] == 200: console.notice("Site %s created succesfully with ID %s" % (name, response["result"]["id"])) return response["result"]
def write_fitfile_to_csv( fitfile, output_file='test_output.csv', original_filename=None, fit_target_dir=None, #raises errors if not defined fit_processed_csv_dir=None, #raises errors if not defined is_overwritten=False, fit_ignore_splits_and_laps=False): local_tz = CST changed_tz = False position_long = None position_lat = None messages = fitfile.messages data = [] lap_data = [] start_data = [] #this should probably work, but it's possibly #based on a certain version of the file/device timestamp = get_timestamp(messages) event_type = get_event_type(messages) if event_type is None: event_type = 'other' output_file = event_type + '_' + timestamp.strftime( '%Y-%m-%d_%H-%M-%S.csv') for m in messages: skip = False skip_lap = False skip_start = False if not hasattr(m, 'fields'): continue fields = m.fields #check for important data types mdata = {} for field in fields: if not changed_tz and field.name in [ 'position_lat', 'position_long', 'start_position_lat', 'start_position_long' ]: if 'lat' in field.name: position_lat = float(field.value) else: position_long = float(field.value) if position_lat is not None and position_long is not None: changed_tz = True tz_name = tzwhere.tzNameAt(position_lat, position_long) local_tz = pytz.timezone(tz_name) if tz_name != 'US/Central': print('Using timezone %s' % tz_name) if field.name in all_allowed_fields: # currently saving timezone conversion to end, but keeping this here for now if field.name == 'timestamp' and False: mdata[field.name] = UTC.localize( field.value).astimezone(local_tz) else: mdata[field.name] = field.value # this is sort of a janky way of determining field type, but it works for now for rf in required_fields: if rf not in mdata: skip = True for lrf in lap_required_fields: if lrf not in mdata: skip_lap = True for srf in start_required_fields: if srf not in mdata: skip_start = True if not skip: data.append(mdata) elif not skip_lap: lap_data.append(mdata) elif not skip_start: start_data.append(mdata) # localize timezone for row in data + lap_data + start_data: if 'timestamp' in row: row['timestamp'] = UTC.localize( row['timestamp']).astimezone(local_tz) #write to csv #general track info with open(os.path.join(fit_processed_csv_dir, output_file), 'w') as f: writer = csv.writer(f) writer.writerow(allowed_fields) for entry in data: writer.writerow([str(entry.get(k, '')) for k in allowed_fields]) if not fit_ignore_splits_and_laps: #lap info with open( os.path.join(fit_processed_csv_dir, lap_filename(output_file)), 'w') as f: writer = csv.writer(f) writer.writerow(lap_fields) for entry in lap_data: writer.writerow([str(entry.get(k, '')) for k in lap_fields]) #start/stop info with open( os.path.join(fit_processed_csv_dir, start_filename(output_file)), 'w') as f: writer = csv.writer(f) writer.writerow(start_fields) for entry in start_data: writer.writerow([str(entry.get(k, '')) for k in start_fields]) print('wrote %s' % output_file) if not fit_ignore_splits_and_laps: print('wrote %s' % lap_filename(output_file)) print('wrote %s' % start_filename(output_file)) if not is_overwritten: append_log(original_filename, fit_processed_csv_dir)
def timezone_extraction(coordinates): print("Extracting timezones") timezone_name = tzwhere.tzNameAt(coordinates[0], coordinates[1]) # Help from: http://stackoverflow.com/questions/15742045/getting-time-zone-from-lat-long-coordinates return timezone_name
_mylon += abs(np.float(val)) / divisor divisor *= 60. mylon = multiplier * _mylon if '--height' in sys.argv: index = np.arange(len(sys.argv))[np.array(sys.argv) == '--height'] height = np.float(np.array(sys.argv)[index + 1].item()) else: warnings.warn('height not provided. Using default 1000 m') height = 1000. mysite = EarthLocation(lat=mylat*u.deg, lon=mylon*u.deg, height=height*u.m) if sitename.split('-')[0] == '': sitename = 'LAT%.1f_LON%.1f' % (mylat, mylon) elif sitename in EarthLocation.get_site_names(): mysite = EarthLocation.of_site(sitename) tzwhere = tzwhere.tzwhere() timezone_str = tzwhere.tzNameAt(mysite.lat.value, mysite.lon.value) timezone = pytz.timezone(timezone_str) dt = datetime.datetime(int(night_starts.split('-')[0]), int(night_starts.split('-')[1]), int(night_starts.split('-')[2]), 0, 0, 0, 0) # only UTC is implemented utcoffset = (timezone.utcoffset(dt).seconds/3600 - 24)*u.hour if (utcoffset.value < 0) & (utcoffset.value >= -12): xlabel = 'Local Time (UTC %i)' % utcoffset.value else: xlabel = 'Local Time (UTC + %i)' % (utcoffset.value + 24) tlabel = 'LT' #utcoffset = 0*u.hour #xlabel = 'UTC' #tlabel = 'UTC'
from pandas import DatetimeIndex from tzwhere import tzwhere from astral import LocationInfo from astral.sun import sun DAY = datetime(2021, 2, 10, tzinfo=pytz.utc) tzwhere = tzwhere.tzwhere() locations = { "Amsterdam": (52.370216, 4.895168), "Tokyo": (35.6684415, 139.6007844), "Dallas": (32.779167, -96.808891), "Cape-Town": (-33.943707, 18.588740), # check southern hemisphere, too } datetimes = [DAY + timedelta(minutes=i * 20) for i in range(24 * 3)] timezones = {k: tzwhere.tzNameAt(*v) for k, v in locations.items()} def irradiance_by_solarpy(latitude: float, longitude: float, dt: datetime, z: str, metric: str = "dni") -> float: """Supports direct horizontal irradiance and direct normal irradiance.""" h = 0 # sea-level dt = dt.astimezone(pytz.timezone(z)).replace(tzinfo=None) # local time dt = solarpy.standard2solar_time(dt, longitude) # solar time if metric == "dhi": # direct horizontal irradiance vnorm = [0, 0, -1] # plane pointing up elif metric == "dni": # direct normal irradiance vnorm = solarpy.solar_vector_ned(
obs, sun = setup(latitude, longitude, altitude) alts = [] azis = [] for thetime in time_utc: obs.date = ephem.Date(thetime) sun.compute(obs) alts.append(np.rad2deg(sun.alt)) azis.append(np.rad2deg(sun.az)) sun_coords['elevation'] = alts sun_coords['azimuth'] = azis return sun_coords lat = 48.866667 lon = 2.333333 tzwhere = tzwhere.tzwhere() tz = tzwhere.tzNameAt(lat, lon) print(tz) day = dt.datetime.today() midnight = day.replace(hour=0, minute=0, second=0, microsecond=0) times = pd.date_range(start=midnight, freq="30min", periods=48) df = get_coordinates(times, lat, lon) file_name = "data/position_th/{}.csv".format(str(day)[:11]) df.to_csv(file_name)
def get_tz_str_from_lat_long(self, lat, long): timezone_str = tzwhere.tzNameAt(lat, long) return timezone_str
import pytz import datetime from tzwhere import tzwhere pd.set_option('display.max_columns', 30) pd.set_option('display.max_rows', 1000) pd.set_option('display.width', 200) df = pd.read_pickle('D:\Krishna\Project\codes\wp_from_radar\data\sar_sm') df['obs_date_local'] = datetime.datetime.now() ll = pd.read_csv( 'D:\Krishna\Project\codes\wp_from_radar\data\scan\query_locations.csv', index_col=0) tzwhere = tzwhere.tzwhere() for index, row in ll.iterrows(): if index == 2062: continue tz_str = tzwhere.tzNameAt(row['Latitude'], row['Longitude']) # Seville coordinates if tz_str: tz = pytz.timezone(tz_str) df.loc[df.site == str(index),'obs_date_local'] = \ pd.to_datetime(df.loc[df.site == str(index),'obs_date']+[tz.utcoffset(dt) for dt in df.loc[df.site == str(index),'obs_date']]) print('[INFO] Time shift done for site %s' % index) else: print('[INFO] Time shifting failed for site %s' % index) df.obs_date_local.apply(lambda x: x.hour).hist() df.to_pickle('D:\Krishna\Project\codes\wp_from_radar\data\sar_sm')
def get_open_tz(location): console.notice(">> Retrieving tz and country code with tzwhere") tz = tzwhere.tzNameAt(location.latitude, location.longitude) country_code = str(location.raw["address"]["country_code"]).upper() return {"tz": tz, "country_code": country_code}
ps.append(0) downwards = True lastheight = h flip_DOWN.append(highesttime) highesttime = t total_dist_up += cur_dist - lastflipdist total_height_up += h - lastflipheight lastflipdist = cur_dist lastflipheight = h hs.append(h) ts.append(t) if pos_lat is not None and pos_long is not None: tzwhere = tzwhere.tzwhere() timezone_str = tzwhere.tzNameAt(180. / 2**31 * float(pos_lat), 180. / 2**31 * float(pos_long)) my_timezone = timezone(timezone_str) else: my_timezone = timezone('Europe/Zurich') print("Distance going down: ", total_dist_down) print("Distance going up: ", total_dist_up) print("") print("Height going down: ", total_height_down) print("Height going up: ", total_height_up) print("") # dates = matplotlib.dates.date2num(ts) maxheight = max(hs) minheight = min(hs)