Esempio n. 1
0
def augment_weather_instances(session, limit=5000, save_every=50):
    """
    Find incomplete `Weather` instances with a location and time and attempt to
    supplement them with historical weather data from the online WSI database.

    Arguments:
        session: A SQLAlchemy scoped session object connected to the database.
        limit: The maximum number of instances to augment (this may be useful
               for complying with API daily usage limits).
        save_every: The number of instances to augment before the session
                    should commit the new data to the disk.
    """
    configure_api_access()
    logger = logging.getLogger()
    logger.info('WSI key set to: {}'.format(wsi.DEFAULT_PARAMETERS['userKey']))

    query = session.query(Weather, Incident.datetime, Operation.ipp_id)
    query = query.join(Incident, Operation)
    query = query.filter(Incident.datetime, Operation.ipp_id)

    columns = Weather.high_temp, Weather.low_temp, Weather.wind_speed
    columns += Weather.snow, Weather.rain, Weather.solar_radiation
    criteria = map(lambda column: column == None, columns)
    query = query.filter(reduce(or_, criteria))

    count = 0
    for weather, datetime_, ipp_id in query:
        ipp = session.query(Point).get(ipp_id)

        if ipp.latitude is not None and ipp.longitude is not None:
            try:
                augment_weather_instance(weather, datetime_, ipp)
                logger.info('Updated {}'.format(weather))
            except ValueError as error:
                logger.error('Instance ID {}: {}'.format(weather.id, error))
            except error:
                logger.error(error)
                break

            count += 1
            if count >= limit:
                break
            elif (count + 1)%save_every == 0:
                session.commit()

    session.commit()
    logger.info('Updated {} weather instances'.format(count))
Esempio n. 2
0
def main():
    """
    Configure online API access and then run the main REPL loop.
    """
    configure_api_access()
    loop()