예제 #1
0
def refresh():
    """Pull fresh data from Open AQ and replace existing data."""
    db.drop_all()
    db.create_all()
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    results = body['results']
    # url = "https://api.openaq.org/v1/measurements?city=Los%20Angeles&parameter=pm25"
    # response = urllib.request.urlopen(url)
    # reader = codecs.getreader("utf-8")
    # obj = json.load(reader(response))
    # datetime_stuff = _finditem(obj, "utc")
    datetime_stuff = []

    for dictionary in results:
        print(type(dictionary))
        for key, value in dictionary.items():
            # print(key, value)
            print(key, value)
            datetime_stuff_utc = _finditem(dictionary, "utc")
            datetime_stuff_value = _finditem(dictionary, "value")
            if not tuple([datetime_stuff_utc, datetime_stuff_value]) in datetime_stuff:
                datetime_stuff.append(tuple([datetime_stuff_utc, datetime_stuff_value]))
            continue
    for j, k in datetime_stuff:
        print(j,k) 
           
    
        r = Record(datetime=j, value=k)
        db.session.add(r)
    db.session.commit()
    return 'Data refreshed!'
예제 #2
0
def get_measurements(city='Los Angeles', parameter='pm25'):
    api = openaq.OpenAQ()
    status, body = api.measurements(city=city, parameter=parameter)
    return [{'utc_datetime': datetime.strptime(result['date']['utc'],
                                               '%Y-%m-%dT%H:%M:%S.%f%z'),
             'location': result['location'],
             'value': result['value']} for result in body['results']]
def refresh():
    """Pull fresh data from Open AQ and replace existing data."""
    # drop current data
    db.drop_all()
    db.create_all()

    # get data from OpenAQ
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')

    data = body["results"]

    # loop through data, create Record objects, and add to db
    i = 1
    for point in data:
        time = point["date"]["utc"]
        value = point["value"]

        # initialize record
        print("Record number:", i)
        db_point = Record.query.get(i) or Record(id=i)

        # assign column values
        print("Assigning values")
        db_point.datetime = time
        db_point.value = value

        # commit record to database
        print("Committing")
        db.session.add(db_point)
        db.session.commit()
        i += 1

    db.session.commit()
    return 'Data refreshed!'
예제 #4
0
def get_city_data(city='Los Angeles',
                  parameter='pm25',
                  date_from=None,
                  limit=1000,
                  max_records=None):
    api = openaq.OpenAQ()
    _, body = api.measurements(
        city=city,
        parameter=parameter,
        date_from=date_from,
        limit=limit,
    )
    pages = body['meta']['pages']
    APP.logger.info(body['meta']['found'])
    results = [(x['date']['utc'], x['value'], x['unit'], x['location'])
               for x in body['results']]

    if pages > 1:
        for i in range(2, pages + 1):
            if max_records and max_records <= len(results):
                break
            APP.logger.info(i)
            _, body = api.measurements(city=city,
                                       parameter=parameter,
                                       date_from=date_from,
                                       page=i,
                                       limit=limit)
            results.extend([(x['date']['utc'], x['value'], x['unit'],
                             x['location']) for x in body['results']])
    return results
예제 #5
0
    def fetch(self, country, limit, start_date, end_date, pollutants, df=True):
        api = openaq.OpenAQ()
        data = self.get_param_dict(country, limit, start_date, end_date,
                                   pollutants, df)

        status, resp = api.measurements(**data)
        no_pages = resp['meta']['found'] // 10000 + 1

        if df == True:
            data['df'] = True

        pollutants_df = pd.DataFrame()
        for index in range(0, no_pages):
            df = api.measurements(**data)
            pollutants_df = pd.concat([pollutants_df, df])
            print("Fetched: page " + str(data['page']))
            data['page'] += 1

        pollutants_df.to_csv(self.get_filename(start_date,
                                               end_date,
                                               directory=self.FETCH_DIR),
                             encoding="utf-8",
                             index=False)

        return pollutants_df
예제 #6
0
        def refresh():
            """Pull fresh data from Open AQ and replace existing data."""
            DB.drop_all()
            DB.create_all()
            # TODO Get data from OpenAQ, make Record objects with it, and add to db
            # Using the query() function to get what I need -
            data = query()
            db_data = (Record.query.get(datetime) or 
                        Record(datetime=data['date'])
            # Adding the data into my
            DB.session.add(db_data)
            DB.session.commit()
            return 'Data refreshed!'



def query():
    '''goes into the API and grabs information needed'''
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    # Setting my i variable
    i=1
    lists = body['results'][0]['date']['utc'], body['results'][0]['value']
    for s in body: 
        li = {body['results'][i]['date']['utc'], body['results'][i]['value']}
        lists.update(li)
        i= i+1
        if i > 100:
            li

    return li

def condition():
    ''' This is going to just filter through my database running it with my flask shell'''
    Record.query.filter(value > 10).all()
예제 #7
0
def googlemap(address):
    address = address.replace(" ", "+")

    config.load_keys()

    current_request = requests.get(
        'https://maps.googleapis.com/maps/api/geocode/json?address=' +
        address + 'key=' + config.keys["GOOGLE_MAPS"])

    current_location = current_request.json()

    if current_location['results']:
        current_city = current_location['results'][0]['address_components'][1][
            'long_name']
        api = openaq.OpenAQ()
        status, resp = api.measurements(city=current_city)
        display = resp['results'][0]
        return {
            "time": display['date']['local'],
            "city": display['city'],
            "location": display['location'],
            "measurement": {
                "parameter": display['parameter'],
                "value": display['value'],
                "unit": display['unit']
            }
        }
    else:
        return {}
예제 #8
0
def get_data(city, parameter):
    """ Query OpenAQ for a city and corresponding air quality value """
    api = openaq.OpenAQ()
    status, body = api.measurements(city=city, parameter=parameter)
    observations = [(obs['date']['utc'], obs['value'])
                    for obs in body['results']]
    return observations
def pull_data():
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    if status != 200:
        return 'Error!'
    else:
        return body['results']
예제 #10
0
def googlemap(current_city):
    
    #address = address.replace(" ", "+") 

    #config.load_keys()

    #current_request = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address='+address+'key='+config.keys["GOOGLE_MAPS"])

    #current_location = current_request.json()
    #print current_location
    
    #if current_location['results']:
    #current_city = current_location['results'][0]['address_components'][1]['long_name']
       # print current_city
        #print "________________________________________________________"
    api = openaq.OpenAQ()
  #  print current_city
    status, resp = api.measurements(city = current_city)
    if status == 200 and resp['results']:
        #print resp['results']
        
        display = resp['results'][0]
        return {"time": display['date']['local'], "city": display['city'], "coordinates": {"lat": display['coordinates']['latitude'], "lng":  display['coordinates']['longitude']}, "location":display['location'], "measurement": {"parameter":display['parameter'], "value":display['value'], "unit": display['unit']}}
    else:
        return {}
예제 #11
0
def create_app():
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db = SQLAlchemy(app)
    db.init_app(app)
    api = openaq.OpenAQ()

    @app.route('/')
    def root():
        return render_template('base.html', title='Home', cities=get_api_cities())

    #     return """<!DOCTYPE html>

    # """

    @app.route('/risky')
    def risky():
        """Datetime, reading where value >= 10"""
        return 'Potentially risky PM2.5 readings<br />' \
               '{}'.format(Record.query.filter(Record.value >= 10).all())

    @app.route('/aq')
    def get_api_data(city='Los Angeles', parameter='pm25'):
        status, body = api.measurements(city=city, parameter=parameter)
        my_list = []
        for date, result in zip(body['results'], body['results']):
            my_list.append(((date['date']['utc']), (result['value'])))
        return my_list

    class Record(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        datetime = db.Column(db.String(25))
        value = db.Column(db.Float, nullable=False)

        def __repr__(self):
            return '-Time {} Reading {}-<br />'.format(self.datetime, self.value)

    @app.route('/refresh')
    def refresh():
        """Pull fresh data from Open AQ and replace existing data."""
        db.drop_all()
        db.create_all()
        new_data = get_api_data()
        for result in new_data:
            db_record = Record(datetime=result[0], value=result[1])
            DB.session.add(db_record)
        db.session.commit()
        return 'Data refreshed!'

    @app.route('/cities')
    def get_api_cities(country="NL", limit=50):
        """ Returns list of dictionaries with city, country, count, number of locations """
        status, body = api.cities(country=country, limit=limit)
        cities_list = []
        for city in body['results']:
            cities_list.append(city['name'])
        return str(cities_list)

    return app
def get_datetimeValues(city, parameter):
    api = openaq.OpenAQ()
    status, body = api.measurements(id=id, city=city, parameter=parameter)
    if status == 200:
        utc_datetimes = [result['date']['utc'] for result in body['results']]
        values = [result['value'] for result in body['results']]
        tuple_list = make_list(utc_datetimes, values)
    return tuple_list
def root():
    """Base view."""
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    n = len(body['results'])
    utcv = [(body['results'][i]['date']['utc'], body['results'][i]['value'])
            for i in range(n)]
    return str(Record.query.filter(Record.value >= 10).all())
 def get_aq_data():
     api = openaq.OpenAQ()
     status, body = api.measurements(city='Los Angeles', parameter='pm25')
     dates_and_values = []
     for i in range(len(body['results'])):
         measurement = (body['results'][i]['date']['utc'], body['results'][i]['value'])
         dates_and_values.append(measurement)
     return dates_and_values
def get_datetime_values(city, parameter):
    api = openaq.OpenAQ()
    status, body = api.measurements(city=city, parameter=parameter)
    if status == 200:
        utc_datetimes = [result['date']['utc'] for result in body['results']]
        values = [result['value'] for result in body['results']]
        list_of_tuples = merge(utc_datetimes, values)
        return list_of_tuples
def get_data():
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    for item in body['results']:
        utc = item['date']['utc']
        value = item['value']
        date_tuple = (utc, value)
        date_time.append(date_tuple)
    return date_time
 def data(city='Los Angeles', parameter='pm25'):
     api = openaq.OpenAQ()
     status, body = api.measurements(city=city, parameter=parameter)
     values = []
     for result in body['results']:
         utc_datatime = result['date']['utc']
         value = result['value']
         values.append((utc_datatime, value))
     return values
def raw_data(city, parameter):
    """
    1. created object api that have the class OpenAQ
    2. api.measurements return two values status and body
    3. return the list of tuples with a loop
    """
    api = openaq.OpenAQ()
    status, body = api.measurements(city=city, parameter=parameter)
    return [(n['date']['utc'], n['value']) for n in body['results']]
예제 #19
0
def get_date_values():
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    results = body['results']
    date_val_tuples = [25]
    for res in results:
        tup = str(res['date']['utc']), res['value']
        date_val_tuples.append(tup)
    return date_val_tuples
예제 #20
0
def cities():
    api = openaq.OpenAQ()
    status, body = api.cities()

    results = getCities(body)
    output = "Cities that OpenAQ covers:\n"
    for city in results:
        output += city + "\n"
    return output
def part2_stretch(lati: float, lngi: float, radi: float) -> str:
    api = openaq.OpenAQ()

    status, resp = api.measurements(coordinates=(lati, lngi),
                                    radius=radi,
                                    date_from='2015-12-20',
                                    date_to='2015-12-29')

    return rtrn_val_utcdt(resp)
def get_records():
    # Set up the API object
    api = openaq.OpenAQ()
    _, body = api.measurements(city='Los Angeles', parameter='pm25')
    results = body['results']
    values = []
    for result in results:
        values.append((result['date']['utc'], result['value']))
    return values
def getdata():
    import openaq
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    list_values = [(item['date']['utc'], item['value'])
                   for item in body['results']]

    #     return 'TODO - part 2 and beyond!'
    return list_values
예제 #24
0
    def get_current_measurement_readings(self):
        """
        Acquire data from the "measurements" API.
        https://docs.openaq.org/#api-Measurements
        """

        # Fetch data from remote API.
        log.info('Requesting measurement data from OpenAQ')

        api = openaq.OpenAQ()

        params = {}
        if self.filter and 'country' in self.filter:
            params['country'] = self.filter['country']

        # TODO: What to do with readings which do not have any geographic information?
        # TODO: Raise limit by introducing result paging.
        date_from = self.last_hour()
        status, response = api.measurements(date_from=date_from,
                                            has_geo=True,
                                            limit=10000,
                                            include_fields=['attribution'],
                                            **params)
        data = response['results']

        if not data:
            log.warning('No records found beginning {} with filter {}'.format(
                date_from, params))
            return

        # Mungle timestamp to be formally in ISO 8601 format (UTC).
        timestamp = data[0]['date']['utc']
        log.info('Timestamp of first record: {}'.format(timestamp))

        # Apply data filter.
        #data = self.apply_filter(data)

        # Transform live API items to actual readings while optionally
        # applying a number of transformation and enrichment steps.
        readings = {}
        for item in self.wrap_progress(data):
            try:
                self.process_measurement(readings, item)

            except Exception as ex:
                log.warning('Could not use observation from {}.\n{}'.format(
                    item, exception_traceback()))

        for reading in readings.values():
            has_data = False
            for observation in reading.observations:
                if observation['data']:
                    has_data = True
                    break
            if has_data:
                yield reading
예제 #25
0
def get_data(city='Los Angeles',
             parameter='pm25'):  # Added default arguments for debugging
    api = openaq.OpenAQ()
    status, body = api.measurements(city=city, parameter=parameter)
    # getting datetime values as strings
    utc = [str(sub['date']['utc']) for sub in body['results']]
    # getting aq values as floats
    vals = [float(sub['value']) for sub in body['results']]
    time_vals = list(zip(utc, vals))
    return time_vals
예제 #26
0
def refresh():
    """Pull fresh data from Open AQ and replace existing data."""
    DB.drop_all()
    DB.create_all()
    # TODO Get data from OpenAQ, make Record objects with it, and add to db
    api = openaq.OpenAQ()
    get_times(api)
    DB.session.commit()
    return render_template("refresh.html", refresh='Data refreshed!')
    return 'Data refreshed!'
예제 #27
0
def locations(city='Los Angeles'):
    """Pull location meta-data from Open AQ and display it."""
    api = openaq.OpenAQ()
    status, body = api.locations(city=city)
    locations = [{'name': loc['location'],
                  'latitude': loc['coordinates']['latitude'],
                  'longitude': loc['coordinates']['longitude']} for loc in body['results']]
    return render_template('locations.html',
                           city=city,
                           locations=locations)
예제 #28
0
def answers():
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    l = []
    for i in range(len(body['results'])):
        a = list(body['results'][i].values())[2:4]
        date, value = list(a[0].values())[0], a[1]
        t = (date, value)
        l.append(t)
    return l
예제 #29
0
def root():
    """Base view."""
    api = openaq.OpenAQ()
    status, body = api.measurements(city='Los Angeles', parameter='pm25')
    results = getDatesValues(body)
    # Part 4 Stretch Goal
    return render_template(
        "base.html", records=Record.query.filter(Record.value >= 10).all())
    return str(Record.query.filter(Record.value >= 10).all())
    return str(results)
def retrieve_data():
    """gets data from OpenAQ api and returns as a string of tuples"""
    api = openaq.OpenAQ()
    test = api.measurements(city='Los Angeles', parameter='pm25')
    number, dict = [i for i in test]
    results_dict = dict['results']
    date_dict = [i['date'] for i in results_dict]
    utc_list = [i['utc'] for i in date_dict]
    value_list = [i['value'] for i in results_dict]
    tuples_list = list(zip(utc_list, value_list))
    return str(tuples_list)