direction = float( data[5]) / 360 # normalize between [0, 1) speed = float(data[6]) / 10 # attempt to normalize return speed, direction except: logger.error("Connection error: reconnecting...") time.sleep(next(wait_time)) return speed, direction LOCATIONS_1 = ['41008', '44005', '46060', '46061'] LOCATIONS_2 = ['46073', '46076', '46077', '46078'] def fetch_data(locations): names = list() values = list() for location in locations: speed, direction = fetch_live_data(location=location) names.append('noaa_wind_speed_' + location + '.json') values.append(speed) names.append('noaa_wind_direction_' + location + '.json') values.append(direction) return names, values if __name__ == '__main__': for locations in [LOCATIONS_1, LOCATIONS_2]: names, values = fetch_data(locations=locations) res = mw.cset(names=names, values=values)
for i, line in enumerate(file): # most recent data is at line number 3 in the file if i == 2: data = line.decode("utf-8").split() if prev_data == data: idx -= 2 else: prev_data = data idx = 6 break for i, line in enumerate(file): if i == idx: direction = float(data[5]) / 360 # normalize between [0, 1) speed = float(data[6]) / 10 # attempt to normalize return speed, direction except: logger.error("Connection error: reconnecting...") time.sleep(next(wait_time)) return speed, direction if __name__=='__main__': utc_now = pytz.utc.localize(datetime.utcnow()) pst_now = utc_now.astimezone(pytz.timezone("America/Los_Angeles")) speed, direction = fetch_live_data() res = mw.cset(names=[NAME_SPEED, NAME_DIR],values=[speed, direction]) pprint({'PST time':pst_now.strftime("%H:%M"),'speed':speed,'direction':direction,"res":res}) print('',flush=True)