def fetch(item): resp = requests.get(API, params={ 'key': zhixin_conf['key'], 'location': LOCATION, 'language': LANGUAGE, 'unit': UNIT, 'days': 1 }) data = resp.json() results = data.get('results', []) print('-------------') if results: result = results[0]['daily'][0] print(result) Weather.upsert_(location_code=item.code, location=item.name, timestamp=result['date'], high=result['high'], low=result['low'], precip=result['precip'], text_day=result['text_day'], text_night=result['text_night'], wind_direction=result['wind_direction'], wind_direction_degree=result['wind_direction_degree'], wind_scale=result['wind_scale'], wind_speed=result['wind_speed']) time.sleep(10)
def run(self): curr_time = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) logger.info("Beginning simulation, press Ctrl+C to exit at any time") logger.info("loading kafka connect jdbc source connector") configure_connector() logger.info("beginning cta train simulation") weather = Weather(curr_time.month) try: while True: logger.debug("simulation running: %s", curr_time.isoformat()) # Send weather on the top of the hour if curr_time.minute == 0: weather.run(curr_time.month) _ = [ line.run(curr_time, self.time_step) for line in self.train_lines ] curr_time = curr_time + self.time_step time.sleep(self.sleep_seconds) except KeyboardInterrupt as e: logger.info("Shutting down") _ = [line.close() for line in self.train_lines]
def find_weathers_data(): query = request.values['location'] weather = Weather(Location(query)) db.session.add(weather) db.session.commit() return jsonify(weather.data())
def post(self): self.parser.add_argument('time') self.parser.add_argument('temp') data = self.parser.parse_args() record = Weather.query.filter_by(time=data.time).first() if record is None: try: weather = Weather(data.time, data.temp) weather.save_to_db() return jsonify(weather.serialize()) except Exception as e: return {'message': e}, 400 else: record.update_in_db(data.temp)
def from_query(data): return Weather(city_name=data["name"], temp=data["main"]["temp"], temp_max=data["main"]["temp_max"], temp_min=data["main"]["temp_min"], feel=data["main"]["feels_like"], humidity=data["main"]["humidity"])
def from_file(data): return Weather(city_name=data["city_name"], temp=data["temp"], temp_max=data["temp_max"], temp_min=data["temp_min"], feel=data["feel"], humidity=data["humidity"])
def test_growth(self): field_1 = Field() crop_1 = Crop(Seed, field_1) self.assertEqual(crop_1.height, 0.0) crop_1.grow(Weather()) self.assertEqual(crop_1.height, 0.002) self.assertNotEqual(field_1.water_density, 1.0)
def index(): field_1 = Field() crop_1 = Crop(Seed, field_1) weather = Weather() data = {} crop_data = [] weather_data = [] for i in range(365): weather_data.append({"temperature": weather.get_daily(i)}) crop_1.grow(weather_data[-1]) crop_data.append({ "height": crop_1.height, "diameter": crop_1.diameter }) data["crop"] = crop_data data["weather"] = weather_data return render_template("test.jinja", dataset=data)
def read_recs(self, filename, fieldnames): """ read recs using streaming protocol """ with open(filename, newline='') as _f: reader = csv.DictReader( _f, dialect='pipe_delim', fieldnames=fieldnames) next(reader, None) # skip header row for row in reader: logging.info('read_recs: row=%s', row) del row[None] # remove unused fields weatherrec = Weather(**row) # use kwargs expansion of the dict weatherrec.date = datetime.strptime(row['date'], '%Y-%m-%d') logging.info('read_recs: weatherrec=%s', weatherrec) yield weatherrec
def construct_weather(city, weather_list): # Init empty lists for data processing weather_data = [] weather_temp_data_points = [] min_temp_list = [] max_temp_list = [] humidity_list = [] time_list = [] # Process raw weather data to construct required data points for x in weather_list: weather_obj = Weather(x['dt_txt'], x['main']['temp_min'], x['main']['temp_max'], x['main']['humidity']) weather_data.append(weather_obj.__dict__) weather_temp_data_points.append(x['main']['temp_min']) weather_temp_data_points.append(x['main']['temp_max']) min_temp_list.append(x['main']['temp_min']) max_temp_list.append(x['main']['temp_max']) humidity_list.append(x['main']['humidity']) time_list.append(x['dt_txt']) # Calculate average temp for periods pre_avg = 0 for x in weather_temp_data_points: pre_avg += x weather_avg = pre_avg / len(weather_temp_data_points) # Calculate median temp for periods weather_median = statistics.median(weather_temp_data_points) # Create graph min temp values as string min_temp_list_string = "" for x in min_temp_list: min_temp_list_string = min_temp_list_string + str(x) + "," min_temp_list_string = min_temp_list_string[:-1] # Create graph max temp values as string max_temp_list_string = "" for x in max_temp_list: max_temp_list_string = max_temp_list_string + str(x) + "," max_temp_list_string = max_temp_list_string[:-1] # Create graph humidity values as string humidity_list_string = "" for x in humidity_list: humidity_list_string = humidity_list_string + str(x) + "," humidity_list_string = humidity_list_string[:-1] # Create x axis labels where 03 = 03:00 x_axis_labels = "" for x in time_list: x_axis_labels = x_axis_labels + "|" + x[11] + x[12] # Generate humidty graph with strings using Google Chart Image API weather_list_len = len(weather_list) chart_width = str(int(weather_list_len * 20.5 + 20)) graph_url = ( "https://chart.googleapis.com/chart?chs=" # + chart_width + "820x300&cht=bvo&chd=t:" + min_temp_list_string + "|" + max_temp_list_string + "|" + humidity_list_string + "&chxl=0:" + x_axis_labels + "&chbh=a&chco=ebbd34,eb4034,80f4ff&chxt=x,y&chxs=0,393939,12,0,lt|1,393939,10,1,lt" ) # Response dict data = { 'status': 200, 'msg': 'Success', 'city': city, 'weather_data_points': weather_data, 'temp_average': round(weather_avg, 2), 'temp_mean': round(weather_median, 2), 'graph_url': graph_url } return data
[float] - reading value """ r = re.search(search_pattern, reading) if r: found = r.group() return (found) else: raise ValueError('reading missing properly formatted numerical value.') # definition of a complete reading temperature_pattern = re.compile("^b'Temperature") humidity_pattern = re.compile("^b'Humidity") # create a new record weather = Weather() # connect to the sensor ser = serial.Serial("/dev/ttyACM0", 9600) # wait for a full reading from the sensor t = False h = False logging.info('Taking reading at {}'.format(str(datetime.now))) while not t or not h: sleep(2) reading = str(ser.readline()) if temperature_pattern.match(reading): weather.temperature = extract_value(reading)
async def main(): weather_model = Weather() tomorrow_data = Weather() day_after_tomorrow = Weather() location = location_finder() or settings.default_location data = await app_repo.get_data(city_name=location) if data: # we need to create three objects of weather model weather_model.set_location = location raw_date = data[0]['created'] formatted_date = datetime.datetime.strftime( dateutil.parser.parse(raw_date), '%b %d %Y, %H:%M:%S') weather_model.set_created_data = formatted_date weather_model.set_minimum_temp = round(data[0]['min_temp'], 1) weather_model.set_temp = round(data[0]['the_temp'], 1) weather_model.set_maximum_temp = round(data[0]['max_temp'], 1) weather_model.set_weather_state = data[0]['weather_state_name'] weather_model.set_pressure = data[0]['air_pressure'] weather_model.set_humidity = data[0]['humidity'] weather_model.set_predictability = data[0]['predictability'] weather_model.set_applicability_date = data[0]['applicable_date'] weather_model.set_weather_abbr = app_repo.set_weather_state_abbr( data[0]['weather_state_abbr']) # Tomorrow tomorrow_data.set_location = location raw_date = data[1]['created'] formatted_date = datetime.datetime.strftime( dateutil.parser.parse(raw_date), '%b %d %Y, %H:%M:%S') tomorrow_data.set_created_data = formatted_date tomorrow_data.set_minimum_temp = round(data[1]['min_temp'], 1) tomorrow_data.set_temp = round(data[1]['the_temp'], 1) tomorrow_data.set_maximum_temp = round(data[1]['max_temp'], 1) tomorrow_data.set_weather_state = data[1]['weather_state_name'] tomorrow_data.set_pressure = data[1]['air_pressure'] tomorrow_data.set_humidity = data[1]['humidity'] tomorrow_data.set_predictability = data[1]['predictability'] tomorrow_data.set_applicability_date = data[1]['applicable_date'] tomorrow_data.set_weather_abbr = app_repo.set_weather_state_abbr( data[1]['weather_state_abbr']) # Tomorrow but one day_after_tomorrow.set_location = location raw_date = data[2]['created'] formatted_date = datetime.datetime.strftime( dateutil.parser.parse(raw_date), '%b %d %Y, %H:%M:%S') day_after_tomorrow.set_created_data = formatted_date day_after_tomorrow.set_minimum_temp = round(data[2]['min_temp'], 1) day_after_tomorrow.set_temp = round(data[2]['the_temp'], 1) day_after_tomorrow.set_maximum_temp = round(data[2]['max_temp'], 1) day_after_tomorrow.set_weather_state = data[0]['weather_state_name'] day_after_tomorrow.set_pressure = data[2]['air_pressure'] day_after_tomorrow.set_humidity = data[2]['humidity'] day_after_tomorrow.set_predictability = data[2]['predictability'] day_after_tomorrow.set_applicability_date = data[2]['applicable_date'] day_after_tomorrow.set_weather_abbr = app_repo.set_weather_state_abbr( data[2]['weather_state_abbr']) click.secho( click.style( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ weather updates ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', fg='black', bold=True)) click.secho( click.style(weather_model.location.title(), bold=True, fg='green')) print('----------------------------------') print() # Today click.secho(click.secho('Today', fg='bright_blue')) if weather_model.weather_state_abbr.lightRain or weather_model.weather_state_abbr.lightRain or weather_model.weather_state_abbr.showers: runner(os.getcwd() + '/rainy.png') elif weather_model.weather_state_abbr.clear or weather_model.weather_state_abbr.lightCloud: runner(os.getcwd() + '/clear.png') elif weather_model.weather_state_abbr.hail or weather_model.weather_state_abbr.snow or weather_model.weather_state_abbr.sleet: runner(os.getcwd() + '/snow.png') elif weather_model.weather_state_abbr.heavyCloud: runner(os.getcwd() + '/cloudy.png') else: runner(os.getcwd() + '/clear.png') print() click.secho(click.style('Weather state', fg='green', bold=True)) print(f'{weather_model.weather_state.title()}') print(f'Max:{weather_model.max_temp} Celsius') print(f'Min:{weather_model.min_temp} Celsius') click.secho(click.style('Humidity', fg='green', bold=True)) print(f'{weather_model.humidity}%') click.secho(click.style('Pressure', fg='green', bold=True)) print(f'{weather_model.pressure}mb') click.secho(click.style('Confidence', fg='green', bold=True)) print(f'{weather_model.predictability}%') # Tomorrow print() click.secho(click.secho('Tomorrow', fg='bright_blue')) print('----------------------------------') if tomorrow_data.weather_state_abbr.lightRain or tomorrow_data.weather_state_abbr.lightRain or tomorrow_data.weather_state_abbr.showers: runner(os.getcwd() + '/rainy.png') elif tomorrow_data.weather_state_abbr.clear or tomorrow_data.weather_state_abbr.lightCloud: runner(os.getcwd() + '/clear.png') elif tomorrow_data.weather_state_abbr.hail or tomorrow_data.weather_state_abbr.snow or tomorrow_data.weather_state_abbr.sleet: runner(os.getcwd() + '/snow.png') elif tomorrow_data.weather_state_abbr.heavyCloud: runner(os.getcwd() + '/cloudy.png') else: runner(os.getcwd() + '/clear.png') print() click.secho(click.style('Weather state', fg='green', bold=True)) print(f'{tomorrow_data.weather_state.title()}') print(f'Max:{tomorrow_data.max_temp} Celsius') print(f'Min:{tomorrow_data.min_temp} Celsius') click.secho(click.style('Humidity', fg='green', bold=True)) print(f'{tomorrow_data.humidity}%') click.secho(click.style('Pressure', fg='green', bold=True)) print(f'{tomorrow_data.pressure}mb') click.secho(click.style('Confidence', fg='green', bold=True)) print(f'{tomorrow_data.predictability}%') # Day after tomorrow print() click.secho( click.secho( f'Day after tomorrow {day_after_tomorrow.applicability_date}', fg='bright_blue')) click.echo('--------------------------------------------------') if day_after_tomorrow.weather_state_abbr.lightRain or day_after_tomorrow.weather_state_abbr.lightRain or day_after_tomorrow.weather_state_abbr.showers: runner(os.getcwd() + '/rainy.png') elif day_after_tomorrow.weather_state_abbr.clear or day_after_tomorrow.weather_state_abbr.lightCloud: runner(os.getcwd() + '/clear.png') elif day_after_tomorrow.weather_state_abbr.hail or day_after_tomorrow.weather_state_abbr.snow or day_after_tomorrow.weather_state_abbr.sleet: runner(os.getcwd() + '/snow.png') elif day_after_tomorrow.weather_state_abbr.heavyCloud: runner(os.getcwd() + '/cloudy.png') else: runner(os.getcwd() + '/clear.png') print() click.secho(click.style('Weather state', fg='green', bold=True)) print(f'{day_after_tomorrow.weather_state.title()}') print(f'Max:{day_after_tomorrow.max_temp} Celsius') print(f'Min:{day_after_tomorrow.min_temp} Celsius') click.secho(click.style('Humidity', fg='green', bold=True)) print(f'{day_after_tomorrow.humidity}%') click.secho(click.style('Pressure', fg='green', bold=True)) print(f'{day_after_tomorrow.pressure}mb') click.secho(click.style('Confidence', fg='green', bold=True)) print(f'{day_after_tomorrow.predictability}%') click.secho(click.style('Data last updated', fg='green', bold=True)) print(formatted_date) else: print( "Well this is embarrassing.The city\'s weather data your are looking for is not locally available & " "could not be loaded from the servers.Here are some solutions: Check your network " "connection and try again (-:")
def test_daily(self): wea = Weather() for i in range(10): print(wea.get_daily(i))
def get(self): try: return jsonify(Weather.get_all(self)) except Exception as e: return {"message": e}, 404
def get_weather(cls, lat, lon): weather_url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=minutely&appid={api_key}&units=metric&lang=ja" with urllib.request.urlopen(weather_url) as res: body = res.read() return Weather(json.loads(body))
def extractWeather(weatherHtml): currentTemperature = __extractCurrentTemperature(weatherHtml) humidity = __extractHumidity(weatherHtml) wind = __extractWind(weatherHtml) return Weather(currentTemperature, humidity, wind)
def __call__(self, *args, **kwargs): """The main program""" print(f'PandasEtlCommand: {args}') verbose = args[0]['--verbose'] if args[0]['--verbose'] else False llevel = logging.DEBUG if verbose else logging.INFO logging.basicConfig(stream=sys.stdout, format='%(asctime)s %(levelname)s %(message)s', level=llevel) if verbose: logging.debug(args) conn_string = args[0]['--conn'] logging.debug(f'setting conn_string to "{conn_string}"') # Populate from file df_with_reserved = pd.read_table( args[0]['--file'], sep='|', header=0, usecols=[ 'id', 'city', 'date', 'actual_mean_temp', 'actual_min_temp', 'actual_max_temp', 'actual_precipitation', 'average_precipitation', 'record_precipitation', 'reserved2' ], dtype={ 'id': 'int', 'city': 'string', # 'date': '?', # let parser handle the converion - see https://stackoverflow.com/questions/21269399/datetime-dtypes-in-pandas-read-csv 'actual_mean_temp': 'int8', 'actual_min_temp': 'int8', 'actual_max_temp': 'int8', # 'actual_precipitation','average_precipitation','record_precipitation', 'reserved2': 'string' }, # These next three are needed to parse and optimize datetime input handling parse_dates=[2], infer_datetime_format=True, date_parser=pd.to_datetime) # Create the engine --> echo=True utiizes the integration with Python logging to display SQL # To make the output more brief set ```echo=False```. engine = create_engine(conn_string, echo=verbose) # We do not want to do this in production - it creates the tables ... Base.metadata.create_all(engine) # Make a session session = sessionmaker(bind=engine)() for _, row in df_with_reserved.iterrows(): weather = Weather(**row) if verbose: logging.debug(weather) session.add(weather) session.commit()