Ejemplo n.º 1
0
 def do(self):
     with open(os.path.join(DATA_DIR, 'waterData.json')) as data_file:
         waterData = json.load(data_file)
         waterData = scripts.byteify(waterData)
     print waterData
     for body in waterData:
         measurement = Measurement(value=body['value'], measured_at=body['dateTime'], body_id=int(body['id']))
         try:
             measurement.save()
         except:
             print "there was a problem saving the measurement"
Ejemplo n.º 2
0
    def handle(self, *args, **options):
        logger.setLevel(logging.INFO)
        handler = logging.StreamHandler()
        handler.setLevel(logging.INFO)
        logger.addHandler(handler)
        formatter = logging.Formatter('%(levelname)s: %(name)s: %(message)s')
        handler.setFormatter(formatter)

        f = open(options['data_file'], 'rb')

        measurements = []
        unrecognised_units = set()
        for line in f:
            datum = json.loads(line)
            datetime = date_parser.parse(datum[0])
            sensor = datum[2]
            if sensor == {}:
                continue
            if '@' not in sensor:
                logger.warning('sensor_id not found in: {}'.format(line))
            sensor_id = sensor['@']
            loop_id = sensor['+'] if '+' in sensor else None
            for key, val in sensor.iteritems():
                if key in ['@', '+']:
                    continue
                split = key.split('|')
                measurement_type = split[0]
                units = split[1] if len(split) == 2 else None
                measurement = Measurement(
                    **{
                        'datetime': datetime,
                        'sensor_id': sensor_id,
                        'loop_id': loop_id,
                        'type': measurement_type,
                        'units': units,
                        'val': val,
                        'val_converted': convert_units(val, units)
                    })
                if measurement.val_converted == None:
                    unrecognised_units.add(units)
                measurements.append(measurement)
                # measurement.save()

        logger.info('measurements: {}'.format(len(measurements)))
        logger.info('measurements[:10]: {}'.format(measurements[:10]))
        logger.info('unrecognised_units: {}'.format(unrecognised_units))

        for i, measurement in enumerate(measurements):
            # print i,
            # sys.stdout.flush()
            try:
                measurement.save()
            except IntegrityError as e:
                logger.error(e)
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        logger.setLevel(logging.INFO)
        handler = logging.StreamHandler()
        handler.setLevel(logging.INFO)
        logger.addHandler(handler)
        formatter = logging.Formatter('%(levelname)s: %(name)s: %(message)s')
        handler.setFormatter(formatter)

        f = open(options['data_file'], 'rb')

        measurements = []
        unrecognised_units = set()
        for line in f:
            datum = json.loads(line)
            datetime = date_parser.parse(datum[0])
            sensor = datum[2]
            if sensor == {}:
                continue
            if '@' not in sensor:
                logger.warning('sensor_id not found in: {}'.format(line))
            sensor_id = sensor['@']
            loop_id = sensor['+'] if '+' in sensor else None
            for key, val in sensor.iteritems():
                if key in ['@', '+']:
                    continue
                split = key.split('|')
                measurement_type = split[0]
                units = split[1] if len(split) == 2 else None
                measurement = Measurement(**{
                    'datetime': datetime,
                    'sensor_id': sensor_id,
                    'loop_id': loop_id,
                    'type': measurement_type,
                    'units': units,
                    'val': val,
                    'val_converted': convert_units(val, units)
                })
                if measurement.val_converted == None:
                    unrecognised_units.add(units)
                measurements.append(measurement)
                # measurement.save()

        logger.info('measurements: {}'.format(len(measurements)))
        logger.info('measurements[:10]: {}'.format(measurements[:10]))
        logger.info('unrecognised_units: {}'.format(unrecognised_units))
        
        for i, measurement in enumerate(measurements):
            # print i,
            # sys.stdout.flush()
            try:
                measurement.save()
            except IntegrityError as e:
                logger.error(e)
Ejemplo n.º 4
0
def add(request):
    if request.method == 'POST':
        form = AddMeasurementToDeviceForm(request.POST, user=request.user)
        if form.is_valid():
            time = form.cleaned_data['times']
            device = form.cleaned_data['devices']
            measurement = form.cleaned_data['measurement']
            new_measurement = Measurement(date=datetime.date.today(), value=measurement)
            new_measurement.time_id = time.id
            new_measurement.device_id = device.id
            new_measurement.facility_id = request.user.facilities.all()[:1].get().id
            new_measurement.save()
            return render(request, 'measurements/index.html')

    form = AddMeasurementToDeviceForm(user=request.user)
    return render(request, 'measurements/add.html', {'form': form})
Ejemplo n.º 5
0
    def handle(self, *args, **options):

        area_data = [[1, "Grand Canyon", 20, 30], [2, "Boca Raton", 40, 50],
                     [3, "Kennesaw", 60, 70], [4, "Mount Hood", 80, 90],
                     [5, "Mount Rainer", 46.5,
                      121.5], [6, "Saint Olaf", 44, 93],
                     [7, "Mount St. Helens", 46, 122]]

        for ad in area_data:
            a = Area(name=ad[1], id=ad[0], longitude=ad[2], latitude=ad[3])
            a.save()

        location_data = [
            [11, "South rim", 200, 1],
            [12, "North rim", 300, 1],
            [13, "Phantom Ranch", 100, 1],
            [14, "Waterfront", 10, 2],
            [15, "Town center", 15, 2],
            [16, "Town center", 310, 3],
            [17, "University", 320, 3],
            [18, "Mall", 330, 3],
            [19, "Airport", 350, 3],
            [20, "South pass", 3500, 4],
            [21, "North rim", 4500, 4],
            [22, "Crater", 4000, 4],
            [23, "North pass", 14000, 5],
            [24, "South glacier", 15000, 5],
            [25, "Ranger station", 16000, 5],
            [26, "Summit", 17000, 5],
            [27, "Yttrboe", 250, 6],
            [28, "Science", 250, 6],
        ]

        no_measurements_location_ids = [23]

        for locd in location_data:
            loc = Location(id=locd[0], name=locd[1], altitude=locd[2])
            a = Area.objects.get(pk=locd[3])
            loc.area = a
            loc.save()

        category_data = [[
            31, "Volcanos", "Areas that are on volcanoes", [4, 5]
        ], [32, "East", "Areas that are in the east", [2, 3]],
                         [33, "West", "Areas that are in the west", [1, 4, 5]],
                         [34, "Wetlands", "Areas that are wetlands", []]]

        for catd in category_data:
            cat = Category(id=catd[0], name=catd[1], description=catd[2])
            cat.save()
            for memb_id in catd[3]:
                a = Area.objects.get(pk=memb_id)
                cat.members.add(a)
            cat.save()

        num_meas_per_loc = 10

        for locd in location_data:
            loc_id = locd[0]
            if loc_id not in no_measurements_location_ids:
                loc = Location.objects.get(pk=loc_id)
                for meas_id in range(loc_id * 100,
                                     loc_id * 100 + num_meas_per_loc):
                    val = random.uniform(5 + loc_id * 3, 15 + loc_id * 3)
                    meas = Measurement(value=val,
                                       id=meas_id,
                                       date=timezone.now())
                    meas.location = loc
                    meas.save()
Ejemplo n.º 6
0
    def handle(self, *args, **options):

        area_data = [
            [1, "Grand Canyon", 20, 30],
            [2, "Boca Raton", 40, 50],
            [3, "Kennesaw", 60, 70],
            [4, "Mount Hood", 80, 90],
            [5, "Mount Rainer", 46.5, 121.5],
            [6, "Saint Olaf", 44, 93],
            [7, "Mount St. Helens", 46, 122]
        ]

        for ad in area_data:
            a = Area(name=ad[1], id=ad[0], longitude=ad[2], latitude=ad[3])
            a.save()

        location_data = [
            [11, "South rim", 200, 1],
            [12, "North rim", 300, 1],
            [13, "Phantom Ranch", 100, 1],
            [14, "Waterfront", 10, 2],
            [15, "Town center", 15, 2],
            [16, "Town center", 310, 3],
            [17, "University", 320, 3],
            [18, "Mall", 330, 3],
            [19, "Airport", 350, 3],
            [20, "South pass", 3500, 4],
            [21, "North rim", 4500, 4],
            [22, "Crater", 4000, 4],
            [23, "North pass", 14000, 5],
            [24, "South glacier", 15000, 5],
            [25, "Ranger station", 16000, 5],
            [26, "Summit", 17000, 5],
            [27, "Yttrboe", 250, 6],
            [28, "Science", 250, 6],
        ]

        no_measurements_location_ids = [23]

        for locd in location_data:
            loc = Location(id=locd[0], name=locd[1], altitude=locd[2])
            a = Area.objects.get(pk=locd[3])
            loc.area = a
            loc.save()

        category_data = [
            [31, "Volcanos", "Areas that are on volcanoes", [4, 5]],
            [32, "East", "Areas that are in the east", [2, 3]],
            [33, "West", "Areas that are in the west", [1, 4, 5]],
            [34, "Wetlands", "Areas that are wetlands", []]
            ]

        for catd in category_data:
            cat = Category(id=catd[0], name=catd[1], description=catd[2])
            cat.save()
            for memb_id in catd[3]:
                a = Area.objects.get(pk=memb_id)
                cat.members.add(a)
            cat.save()

        num_meas_per_loc = 10

        for locd in location_data:
            loc_id = locd[0]
            if loc_id not in no_measurements_location_ids:
                loc = Location.objects.get(pk=loc_id)
                for meas_id in range(loc_id*100, loc_id*100+num_meas_per_loc):
                    val = random.uniform(5+loc_id*3, 15+loc_id*3)
                    meas = Measurement(value=val, id=meas_id, date=timezone.now())
                    meas.location = loc
                    meas.save()