Ejemplo n.º 1
0
def get_WHO_exceedances(location, pollutant='PM 2.5', frequency='daily'):
    '''
    Returns exceedances based on the WHO limit
    '''
    device_code = code_locations_dict[location]
    end_date = datetime.now()
    start_date = end_date - timedelta(days=29)

    records = get_filtered_data(device_code, helpers.date_to_str(start_date),
                                helpers.date_to_str(end_date), frequency,
                                pollutant)

    if pollutant == 'NO2':
        exceedance_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['no2Conc']['value'] > 40)

    elif pollutant == 'PM 10':
        exceedance_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm10ConcMass']['value'] > 50)

    else:
        exceedance_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm2_5ConcMass']['value'] > 25.0)
    return exceedance_sum
Ejemplo n.º 2
0
def calculate_average_daily_measurements_for_last_28_days():
    ms = monitoring_site.MonitoringSite()
    gr = graph.Graph()
    monitoring_sites = list(
        app.mongo.db.monitoring_site.find({}, {
            "DeviceCode": 1,
            "Parish": 1,
            "LocationCode": 1,
            "Division": 1,
            "_id": 0
        }))

    devices_historical_records = []
    for monitoring_site_device in monitoring_sites:
        print(monitoring_site_device)
        code = monitoring_site_device['DeviceCode']
        historical_results = []
        records = []
        pm25_daily_values = []
        average_pm25 = 0
        if code:  #check if code is not empty
            print(code)
            parish = monitoring_site_device['Parish']
            division = monitoring_site_device['Division']
            location_code = monitoring_site_device['LocationCode']
            created_at = helpers.str_to_date(
                helpers.date_to_str(datetime.now()))

            endtime = helpers.date_to_str(datetime.now())
            starttime = helpers.date_to_str(datetime.now() -
                                            timedelta(days=28))
            monitoring_site_measurements_cursor = gr.get_filtered_data(
                code, starttime, endtime, 'daily', 'PM 2.5')
            #monitoring_site_measurements_cursor = ms.get_device_past_28_days_measurements(code)
            for site in monitoring_site_measurements_cursor:
                record = {
                    'pm2_5_value': int(site['pollutant_value']),
                    'time': site["time"]
                }
                records.append(record)
                pm25_daily_values.append(int(site['pollutant_value']))
                historical_results.append(site)

            if len(pm25_daily_values) > 0:
                average_pm25 = np.mean(pm25_daily_values)
                historical_record = {
                    'deviceCode': code,
                    'average_pm25': average_pm25,
                    'historical_records': records,
                    'Parish': parish,
                    'Division': division,
                    'LocationCode': location_code,
                    'created_at': created_at
                }
                devices_historical_records.append(historical_record)

    mongo_helpers.save_device_daily_historical_averages(
        devices_historical_records)

    return jsonify({'response': 'all new hourly measurements saved'}), 200
Ejemplo n.º 3
0
def get_device_past_28_days_measurements():
    ms = monitoring_site.MonitoringSite()
    gr = graph.Graph()
    if request.method == 'GET':
        device_code = request.args.get('device_code')
        if device_code:
            historical_results = []
            records = []
            pm25_daily_values = []
            endtime = helpers.date_to_str(datetime.now())
            starttime = helpers.date_to_str(datetime.now() -
                                            timedelta(days=28))
            monitoring_site_measurements_cursor = gr.get_filtered_data(
                device_code, starttime, endtime, 'daily', 'PM 2.5')
            #monitoring_site_measurements_cursor = ms.get_device_past_28_days_measurements(device_code)
            for site in monitoring_site_measurements_cursor:
                record = {
                    'pm2_5_value': int(site['pollutant_value']),
                    'time': site["time"]
                }
                records.append(record)
                pm25_daily_values.append(int(site['pollutant_value']))
                historical_results.append(site)
            return jsonify({
                "historical_measurements": historical_results,
                "records": records,
                "pm25_values": pm25_daily_values
            })
        else:
            return jsonify({
                "error msg":
                "device code wasn't supplied in the query string parameter."
            })
Ejemplo n.º 4
0
    def get_device_past_28_days_measurements(self, device_code):
        """
        Gets all the daily measurements for the deviceo for the past 28 days from current day. 

        Args:
            device_code: the code of the devices whose measurements are to be returned. 

        Returns:
            A list of the daily measurements for the past 28 days.
        """
        endtime = helpers.date_to_str(datetime.now())
        starttime = helpers.date_to_str(datetime.now() - timedelta(days=28))        
        results = mongo_helpers.get_filtered_data(device_code,starttime, endtime,'daily','PM 2.5')
        #results = list(app.mongo.db..find({"deviceCode":device_code}))
        return results
Ejemplo n.º 5
0
    def get_all_devices_past_28_days_exceedences(self,
                                                 pollutant='PM 2.5',
                                                 standard='AQI'):
        """
        Gets all the exceedences for all the locations in the past 28 days from current day. 

        Args:
            pollutant: the pollutant whose exceedences are to be returned. 
            standard: the standard to use to get the exceedences.

        Returns:
            A list of the number of daily exceedences for the specified pollutant and standard in the past 28 days.
        """
        created_at = helpers.str_to_date(
            helpers.date_to_str(datetime.now().date()))
        # print(created_at)
        query = {
            '$match': {
                'created_at': {
                    '$gte': created_at
                },
                'pollutant': pollutant,
                'standard': standard
            }
        }
        projection = {'$project': {'_id': 0}}
        results = list(
            app.mongo.db.device_daily_exceedences.aggregate(
                [query, projection]))
        return results
Ejemplo n.º 6
0
def get_device_codes():
    devices_codes = list(mongo.db.devices.find({}, {"code": 1, "_id": 0}))
    devices_codes_list = []
    for device_code in devices_codes[0:2]:
        last_time = mongo_helpers.get_last_time_from_device_hourly_measurements(
            device_code['code'])
        start_time = helpers.date_to_str(
            helpers.str_to_date(last_time) + timedelta(hours=1))
        devices_codes_list.append({
            "code": device_code['code'],
            "start time": start_time,
            "last time": last_time
        })
    return jsonify({'device codes': devices_codes_list}), 200
Ejemplo n.º 7
0
    def get_all_devices_past_28_days_measurements(self):
        """
        Gets all the daily measurements for the deviceo for the past 28 days from current day. 

        Args:
            device_code: the code of the devices whose measurements are to be returned. 

        Returns:
            A list of the daily measurements for the past 28 days.
        """       
        created_at = helpers.str_to_date(helpers.date_to_str(datetime.now().date()))
        print(created_at)
        query = {'$match':{ 'created_at': {'$gte': created_at} }}
        projection = { '$project': { '_id': 0 }}
        results = list(app.mongo.db.device_daily_historical_averages.aggregate([query, projection]) )       
        return results
Ejemplo n.º 8
0
def get_AQI_exceedances(location, pollutant='PM 2.5', frequency='daily'):
    '''
    Returns exceedances based on the AQI index
    '''
    device_code = code_locations_dict[location]
    end_date = datetime.now()
    start_date = end_date - timedelta(days=29)

    records = get_filtered_data(device_code, helpers.date_to_str(start_date),
                                helpers.date_to_str(end_date), frequency,
                                pollutant)

    if pollutant == 'NO2':
        UH4SG_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['no2Conc']['value'] > 100
            and records[i]['characteristics']['no2Conc']['value'] <= 360)
        unhealthy_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['no2Conc']['value'] > 360
            and records[i]['characteristics']['no2Conc']['value'] <= 649)
        v_unhealthy_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['no2Conc']['value'] > 649
            and records[i]['characteristics']['no2Conc']['value'] <= 1249)
        hazardous_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['no2Conc']['value'] > 1249
            and records[i]['characteristics']['no2Conc']['value'] <= 2049)

    elif pollutant == 'PM 10':
        UH4SG_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm10ConcMass']['value'] > 154
            and records[i]['characteristics']['pm10ConcMass']['value'] <= 254)
        unhealthy_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm10ConcMass']['value'] > 254
            and records[i]['characteristics']['pm10ConcMass']['value'] <= 354)
        v_unhealthy_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm10ConcMass']['value'] > 354
            and records[i]['characteristics']['pm10ConcMass']['value'] <= 424)
        hazardous_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm10ConcMass']['value'] > 424
            and records[i]['characteristics']['pm10ConcMass']['value'] <= 604)
    else:
        UH4SG_sum = sum(
            1 for i in range(len(records))
            if records[i]['characteristics']['pm2_5ConcMass']['value'] > 35.4
            and records[i]['characteristics']['pm2_5ConcMass']['value'] <= 55.4
        )
        unhealthy_sum = sum(
            1 for i in range(len(records)) if
            records[i]['characteristics']['pm2_5ConcMass']['value'] > 55.4 and
            records[i]['characteristics']['pm2_5ConcMass']['value'] <= 150.4)
        v_unhealthy_sum = sum(
            1 for i in range(len(records)) if
            records[i]['characteristics']['pm2_5ConcMass']['value'] > 150.4 and
            records[i]['characteristics']['pm2_5ConcMass']['value'] <= 250.4)
        hazardous_sum = sum(
            1 for i in range(len(records)) if
            records[i]['characteristics']['pm2_5ConcMass']['value'] > 250.4 and
            records[i]['characteristics']['pm2_5ConcMass']['value'] <= 500.4)

    #exceedances = {'UHSG':UH4SG_sum, 'Unhealthy':unhealthy_sum, 'Very Unhealthy':v_unhealthy_sum, 'Hazardous':hazardous_sum}
    exceedances = [UH4SG_sum, unhealthy_sum, v_unhealthy_sum, hazardous_sum]
    return exceedances