Пример #1
0
 def dailyToutKWh(self):
     [datesA, wattsA] = self.building.dailyData
     fig = Figure(facecolor='white', edgecolor='none')
     ax = fig.add_subplot(111)
     # multiple the mean by 24 hrs to get kWh - this is independent of observation interval
     daySum = self.building.dailyStats['mean'] * 24 / 1000
     wd = WeatherData('weather')
     (dates, tout) = wd.matchWeather(self.building.days, self.building.zip5)
     #ax.plot(dts,daySum,'o',color='#000000',alpha=1,label='Daily kWh')
     #ax.set_xlabel('Date')
     ax.set_xlabel('Mean daily temperature (F)')
     ax.plot(tout,
             daySum,
             'o',
             color='#9970AB',
             alpha=1,
             label='Weekday kWh')
     # weekend dates
     wknd = np.where([int(dt.isoweekday() > 5) for dt in dates])[0].tolist()
     print wknd
     ax.plot(np.array(tout)[wknd],
             np.array(daySum)[wknd],
             'o',
             color='#5AAE61',
             alpha=1,
             label='Weekend kWh')
     # todo: identify weekends and color differently
     ax.set_title('Daily energy (kWh)')
     ax.set_ylabel('kWh')
     ax.grid(True)
     ax.legend(loc='upper right')
     ax.yaxis.set_major_formatter(mplt.FormatStrFormatter('%0.1f'))
     return fig
Пример #2
0
def index():
    if request.method == 'POST':
        new_temperature = request.form['temperature']
        new_humidity = request.form['humidity']
        new_pressure = request.form['pressure']

        try:
            newWeather = WeatherData(temperature=new_temperature,
                                     humidity=new_humidity,
                                     pressure=new_pressure)
            newWeatherDict = newWeather.convertToDict()

            mongoCol.insert_one(newWeatherDict)

            return redirect('/')
        except:
            return 'There was an issue adding your data'
    else:  #Get request
        cursor = mongoCol.find()  # make cursor with no sorting
        listOfWeatherData = list(
            cursor
        )  # return all the objects as a list and set the cursor to last index

        listOfWeatherData = convertObjectId(listOfWeatherData)  # See function

        return json.dumps(listOfWeatherData)
Пример #3
0
def post_weather(current_user):
    data = request.get_json()
    """ STRUCTURE:
	{
		"place": {
			"name": "Aarhus",
			"lat": 58.986462,
			"lon": 6.190466
		},
		"temp": 13.231,
		"humidity": 54,
		"press": 1030
	}"""

    try:
        new_place = Place(name=data['place']['name'],
                          lat=data['place']['lat'],
                          lon=data['place']['lon'])
        new_weather = WeatherData(place=new_place,
                                  temperature=data['temp'],
                                  humidity=data['humidity'],
                                  pressure=data['press'])
        weatherDict = new_weather.convertToDict()

        socketio.emit('new data', weatherDict)

        weatherCol.insert_one(weatherDict)

        return jsonify({"message": "Posted new weatherdata!"}), 200
    except:
        return jsonify({"message": "Error in posting weatherdata!"}), 400
Пример #4
0
    def validate(self, params):
        errs = {}
        if params.get("upFile").file is None:
            errs["upFile"] = "No file selected for upload"
        required = {
            "bldg_name": "Missing building name",
            "bldg_type": "Missing building type",
            "bldg_vintage": "Missing year of construction",
            "hvac_type": "Missing heating and cooling information",
            "occ_count": "Missing occupant count",
            "bldg_zip": "Missing zip code",
            "bldg_size": "Missing square footage"
        }

        numeric = {
            "bldg_zip": "Zip must be 5 digits (only)",
            "bldg_vintage": "Vintage must be a valid year of 4 digits (only)",
            "bldg_size": "Square footage must be a positive number",
            "occ_count": "Occupancy must be a positive number"
        }

        for key in required:
            val = params.get(key, "").lower().strip()
            val = val.replace(
                '-', '')  # remove dashes, which are used as separators
            val = val.replace(
                'choose one',
                '',
            )
            val = val.replace(
                'select one',
                '',
            )
            if val == "": errs[key] = required[key]

        for key in numeric:
            if params.get(key, "") == '?':
                continue  # ? is an indicator that the user is unsure of the value
            try:
                intVal = int(float(params.get(key, "foo")))
                if intVal < 0: raise ValueError("Must be positive")
            except ValueError as ve:
                errs[key] = numeric[key]

        if len(params.get("bldg_zip")) != 5:
            errs["bldg_zip"] = "Zip code must be 5 digits"

        if "bldg_zip" not in errs.keys():
            weather = WeatherData("weather")  # dataDir
            zipLatLon = weather.zipMap().get(int(params["bldg_zip"]), None)
            if zipLatLon is None:
                errs[
                    "bldg_zip"] = "Invalid zip code not found in national list. Try another nearby?"
        return errs
Пример #5
0
class TestWeatherData(unittest.TestCase):
    def setUp(self):
        self.wd = WeatherData()

    def testGetWindVelocity(self):
        # setup
        # define start/stop times
        start = (2007, 11, 30, 0, 0, 0)
        end = (2007, 12, 30, 0, 0, 0)
        expMJD = [
            54434.00000041, 54434.00001201, 54434.00002358, 54434.00003515
        ]
        expWind = [2.57343006, 2.87351751, 2.85987568, 2.87351751]

        # test
        time, wind = self.wd.getWindVelocity((start, end))

        for i in range(4):
            self.assertAlmostEquals(expMJD[i], time[i], 2)
            self.assertAlmostEquals(expWind[i], wind[i], 2)

    def testGetLastHourMedianWindSpeeds(self):

        # for when?
        now = datetime(2007, 11, 30, 0, 0, 0)

        # test a specific time
        wind = self.wd.getLastHourMedianWindSpeeds(now)

        self.assertAlmostEquals(2.38246560, wind, 4)

        # make sure it changes by looking at the *current* wind
        wind = self.wd.getLastHourMedianWindSpeeds()

        self.assertNotEqual(2.38246560, wind)

    def testHourDanaMedianSpeeds(self):

        dt = datetime(2010, 6, 7, 12)  # UTC
        m = self.wd.getHourDanaMedianSpeeds(dt)
        self.assertAlmostEquals(3.74649739265, m, 4)

        dt = datetime(2009, 6, 7, 12)  # UTC
        m = self.wd.getHourDanaMedianSpeeds(dt)
        self.assertAlmostEquals(0.52738451957702637, m, 4)

    def testDanaMedian(self):

        # simple tests first
        data = [1.0 for i in range(3600)]
        m = self.wd.danaMedian(data)
        self.assertEqual(1.0, m)

        data = [1.0 for i in range(110)]
        m = self.wd.danaMedian(data)
        self.assertEqual(1.0, m)

        data = [float(i) for i in range(3600)]
        m = self.wd.danaMedian(data)
        self.assertEqual(3249.5, m)
Пример #6
0
class WeatherStation2Text:
    """
    This class contains logic to print 
    weather station 2 wind speeds using the WeatherData class
    """
    def __init__(self, year):
        self.dtFormat = "%Y-%m-%d %H:%M:%S"
        self.weatherData = WeatherData()
        self.start = datetime(year, 1, 1, 0, 0, 0)
        self.end = self.start + timedelta(hours=24 * 365)

    def getWind(self, dt):
        """
        Just a wraper method to avoid using long ass method names
        and format the data.
        """
        return dt, self.weatherData.getLastHourMedianWindSpeeds(dt)

    def getWinds(self, hours):
        retval = []
        for h in range(hours):
            dt = self.start + timedelta(hours=h)
            try:
                retval.append(self.getWind(dt))
            except:
                pass
        return retval

    def print_winds(self, data):
        print "MJD Measured"
        for dt, m in data:
            mjd = dt2mjd(dt)
            print mjd, m
Пример #7
0
class WeatherStation2Text:
    """
    This class contains logic to print 
    weather station 2 wind speeds using the WeatherData class
    """
    def __init__(self, year):
        self.dtFormat = "%Y-%m-%d %H:%M:%S"
        self.weatherData = WeatherData()
        self.start = datetime(year, 1, 1, 0, 0, 0)
        self.end = self.start + timedelta(hours = 24*365)

    def getWind(self, dt):
        """
        Just a wraper method to avoid using long ass method names
        and format the data.
        """
        return dt, self.weatherData.getLastHourMedianWindSpeeds(dt)

    def getWinds(self, hours):
        retval = []
        for h in range(hours):
            dt = self.start + timedelta(hours = h)
            try:
                retval.append(self.getWind(dt))
            except:
                pass
        return retval

    def print_winds(self, data):
        print "MJD Measured"
        for dt, m in data:
            mjd = dt2mjd(dt)
            print mjd, m
Пример #8
0
 def __init__(self, dbname = ""):
     self.c           = pg.connect(user = "******"
                                 , dbname = dbname
                                 , port   = settings.DATABASE_PORT
                                 )
     self.weatherData = WeatherData()
     self.pyrgeometerData = PyrgeometerData()
Пример #9
0
 def __init__(self):
     weatherData = WeatherData()
     currentConditionsDisplay = CurrentConditionsDisplay(weatherData)
     forecastDisplay = ForecastDisplay(weatherData)
     statisticsDisplay = StatisticsDisplay(weatherData)
     
     weatherData.setMeasurements(80, 65, 30.4)
     print("\n")
     weatherData.setMeasurements(82, 70, 29.3)
     print("\n")
     weatherData.setMeasurements(78, 90, 26.4)
Пример #10
0
class WeatherStation2DBImport:
    """
    This class contains lodgic to populate the weather database with
    weather station 2 wind speeds using the WeatherData class
    """
    def __init__(self, dbname = "weather"):
        self.c           = pg.connect(user = "******"
                                    , dbname = dbname)
        self.weatherData = WeatherData()

    def getWind(self, dt):
        """
        Just a wraper method to avoid using long ass method names
        and format the data.
        """
        dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
        return dt, self.weatherData.getLastHourMedianWindSpeeds(dt)

    def getWindSpeeds(self):
        """
        Gets the wind speeds from weather station 2 data for every hour
        in the weather database that does not have a weather station 2
        record to now.
        """
        r = \
            self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM weather_station2)
                               AND date <= '%s'
                         """ % datetime.utcnow())
        return [self.getWind(row['date']) for row in r.dictresult()]

    def getWeatherDate(self, dt):
        r = \
            self.c.query("SELECT id FROM weather_dates WHERE date = '%s'" % dt)
        if len(r.dictresult()) == 0:
            self.c.query("INSERT INTO weather_dates (date) VALUES ('%s')" % dt)
            r = \
                self.c.query("SELECT id FROM weather_dates WHERE date = '%s'" % dt)
        return r.dictresult()[0]["id"]

    def insert(self):
        for dt, wind_speed in self.getWindSpeeds():
            wd_id = self.getWeatherDate(dt)
            # Handle if no weather station wind speed measure was taken
            if str(wind_speed) == 'nan':
                earlier = datetime.utcnow() - timedelta(hours = 4)
                if dt < earlier:
                    wind_speed = "NULL"
            self.c.query("""
                         INSERT INTO weather_station2 (wind_speed, weather_date_id)
                         VALUES (%s, %s)
                         """ % (wind_speed, wd_id))
Пример #11
0
class WeatherStation2DBImport:
    """
    This class contains lodgic to populate the weather database with
    weather station 2 wind speeds using the WeatherData class
    """
    def __init__(self, dbname="weather"):
        self.c = pg.connect(user="******", dbname=dbname)
        self.weatherData = WeatherData()

    def getWind(self, dt):
        """
        Just a wraper method to avoid using long ass method names
        and format the data.
        """
        dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
        return dt, self.weatherData.getLastHourMedianWindSpeeds(dt)

    def getWindSpeeds(self):
        """
        Gets the wind speeds from weather station 2 data for every hour
        in the weather database that does not have a weather station 2
        record to now.
        """
        r = \
            self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM weather_station2)
                               AND date <= '%s'
                         """ % datetime.utcnow())
        return [self.getWind(row['date']) for row in r.dictresult()]

    def getWeatherDate(self, dt):
        r = \
            self.c.query("SELECT id FROM weather_dates WHERE date = '%s'" % dt)
        if len(r.dictresult()) == 0:
            self.c.query("INSERT INTO weather_dates (date) VALUES ('%s')" % dt)
            r = \
                self.c.query("SELECT id FROM weather_dates WHERE date = '%s'" % dt)
        return r.dictresult()[0]["id"]

    def insert(self):
        for dt, wind_speed in self.getWindSpeeds():
            wd_id = self.getWeatherDate(dt)
            # Handle if no weather station wind speed measure was taken
            if str(wind_speed) == 'nan':
                earlier = datetime.utcnow() - timedelta(hours=4)
                if dt < earlier:
                    wind_speed = "NULL"
            self.c.query("""
                         INSERT INTO weather_station2 (wind_speed, weather_date_id)
                         VALUES (%s, %s)
                         """ % (wind_speed, wd_id))
Пример #12
0
    def search_location(self):
        weather_obj = WeatherData(self.city_entry.text(),
                                  self.state_entry.text())
        weather_obj.get_weather_data()

        if weather_obj.data == 0:
            self.error_msg(1)
        if 'results' in weather_obj.data:
            self.error_msg(2)

        else:
            print(weather_obj.data['local_time_rfc822'])
            self.loclb_value.setText(
                weather_obj.data["display_location"]["full"])
            self.datelb_value.setText(
                weather_obj.data['local_time_rfc822'][:-6])
            self.templb_value.setText(str(weather_obj.data['temp_f']))

            self.condlb_value.setText(weather_obj.data['weather'])
            self.windspdlb_value.setText(str(weather_obj.data['wind_mph']))
            self.winddirlb_value.setText(weather_obj.data['wind_dir'])
  def validate(self,params):
    errs = {}
    if params.get("upFile").file is None: errs["upFile"] = "No file selected for upload"
    required = {  "bldg_name"    : "Missing building name",
                  "bldg_type"    : "Missing building type",
                  "bldg_vintage" : "Missing year of construction",
                  "hvac_type"    : "Missing heating and cooling information",
                  "occ_count"    : "Missing occupant count",
                  "bldg_zip"     : "Missing zip code",
                  "bldg_size"    : "Missing square footage"     }
    
    numeric = { "bldg_zip"      : "Zip must be 5 digits (only)",
                "bldg_vintage"  : "Vintage must be a valid year of 4 digits (only)",
                "bldg_size"     : "Square footage must be a positive number",
                "occ_count"     : "Occupancy must be a positive number" }

    
    for key in required:
      val = params.get(key,"").lower().strip()
      val = val.replace('-','') # remove dashes, which are used as separators
      val = val.replace('choose one','',)
      val = val.replace('select one','',)
      if val == "": errs[key] = required[key]
    
    for key in numeric:
      if params.get(key,"") == '?': continue # ? is an indicator that the user is unsure of the value
      try: 
        intVal = int( float( params.get(key,"foo") ) )
        if intVal < 0: raise ValueError("Must be positive")
      except ValueError as ve: errs[key] = numeric[key]

    if len(params.get("bldg_zip")) != 5: errs["bldg_zip"] = "Zip code must be 5 digits"
    
    if "bldg_zip" not in errs.keys():
      weather = WeatherData("weather") # dataDir
      zipLatLon = weather.zipMap().get(int(params["bldg_zip"]),None)
      if zipLatLon is None: errs["bldg_zip"] = "Invalid zip code not found in national list. Try another nearby?"
    return errs
Пример #14
0
 def dailyToutKWh(self):
   [datesA,wattsA] = self.building.dailyData
   fig = Figure(facecolor='white',edgecolor='none')
   ax = fig.add_subplot(111)
   # multiple the mean by 24 hrs to get kWh - this is independent of observation interval
   daySum  = self.building.dailyStats['mean']*24/1000
   wd = WeatherData('weather')
   (dates,tout) = wd.matchWeather(self.building.days,self.building.zip5)
   #ax.plot(dts,daySum,'o',color='#000000',alpha=1,label='Daily kWh')
   #ax.set_xlabel('Date')
   ax.set_xlabel('Mean daily temperature (F)')
   ax.plot(tout,daySum,'o',color='#9970AB',alpha=1,label='Weekday kWh')
   # weekend dates
   wknd = np.where([int(dt.isoweekday() > 5) for dt in dates])[0].tolist()
   print wknd
   ax.plot(np.array(tout)[wknd],np.array(daySum)[wknd],'o',color='#5AAE61',alpha=1,label='Weekend kWh')
   # todo: identify weekends and color differently
   ax.set_title('Daily energy (kWh)')
   ax.set_ylabel('kWh')
   ax.grid(True)
   ax.legend(loc='upper right')
   ax.yaxis.set_major_formatter(mplt.FormatStrFormatter('%0.1f'))
   return fig
Пример #15
0
    def parseData(self):
        # check if the listOfCities has been populated
        if not self.listOfCityData:
            return False

        else:
            # loop over list and parse the data
            for index in range(len(self.listOfCityData)):
                # create a new object of weather data
                newWeatherData = WeatherData()

                # loop over the dictionary values in the list
                for key, value in self.listOfCityData[index].iteritems():
                    # grab weather Description key: "weather" (String)
                    if key == "weather":
                        # print value
                        newWeatherData.weatherCondition = value

                    # grab the location key: "location" (String)
                    if key == "location":
                        # print value
                        newWeatherData.location = value

                    # grab temp key: "temperature_string" (String)
                    if key == "temperature_string":
                        # print value
                        newWeatherData.currentTemp = value

                    # grab Humidity key: "relative_humidity" (Int)
                    if key == "relative_humidity":
                        # print value
                        newWeatherData.humidity = value

                    # grab wind direction key: "wind_dir" (String)
                    if key == "wind_dir":
                        # print value
                        newWeatherData.windDirection = value

                    # grab last updated time key: "observation_time" (String)
                    if key == "observation_time":
                        # print value
                        newWeatherData.lastUpdated = value

                # append the new weather data object to the list
                self.listOfParsedWeatherData.append(newWeatherData)

        # return success from the function
        return True
def on_message(client, userdata, msg):

    # Obtem os dados vindos do Broker/MQTT
    newTopic = msg.topic.split("/", 1)
    msg.payload = msg.payload.decode("utf-8")

    # Obtem do Banco o Id do respectivo sensor
    sensor = selectSensorNick(connection, newTopic[1])

    # A partir dos dados obtidos anteriormente, armazena as informacoes na respectiva tabela
    weatherData = WeatherData(int(999), sensor.id, msg.payload)
    insertWeatherData(connection, weatherData)
    updateRowDefaultConf(connection, weatherData)

    print("\n")
    print("MQTT Data Received...")
    print("MQTT Topic: " + newTopic[0])
    print("MQTT Sensor: " + newTopic[1])
    print("Data: " + str(msg.payload))
    print("\n")
Пример #17
0
        self.move_servo(servo_position)
        self.set_led_status(led_status)

    def move_servo(self, servo_position=0):
        self.servo.value = self.convert_percentage_to_integer(servo_position)

    def turnOffServo(self):
        sleep(2)
        self.servo.close()

    def set_led_status(self, led_status=0):

        if (led_status == 0):
            self.led.off()
        elif (led_status == 1):
            self.led.on()
        else:
            self.led.blink()

    def convert_percentage_to_integer(self, percentage_amount):
        return (percentage_amount * 0.02) - 1


if __name__ == "__main__":

    weather_data = WeatherData('Toronto')
    weather_dashboard = WeatherDashboard(weather_data.getServoValue(),
                                         weather_data.getLEDValue())
    weather_dashboard.turnOffServo()
Пример #18
0
 def setUp(self):
     self.wd = WeatherData()
Пример #19
0
def on_message(client, userdata, msg):

    # Obtem os dados vindos do Broker/MQTT
    newTopic = msg.topic.split("/", 1)
    msg.payload = msg.payload.decode("utf-8")

    if newTopic[1] == "rowwantcall":
        # Obtem do Banco o Id da respectiva linha
        row = selectRowNick(connection, newTopic[0])
        rowDefaultConf = selectRowDefaultConf(connection, row.getId())

        MQTT_Topic_AjustTemp = qc + "/" + environment + "/ajustprogtemp"
        AjustTemp_Fake_Value = float("{0:.1f}".format(rowDefaultConf.getProgTemp()))
        publish_To_Topic(MQTT_Topic_AjustTemp, AjustTemp_Fake_Value)

        MQTT_Topic_AjustStatus = qc + "/" + environment + "/ajuststatus"
        AjustStatus_Fake_Value = rowDefaultConf.getStatus()
        publish_To_Topic(MQTT_Topic_AjustStatus, AjustStatus_Fake_Value)

    else:

        # Obtem do Banco o Id da respectiva linha
        row = selectRowNick(connection, newTopic[0])

        # Obtem do Banco o Id do respectivo sensor
        sensor = selectSensorNick(connection, newTopic[1])

        # A partir dos dados obtidos anteriormente, armazena as informacoes na respectiva tabela
        weatherData = WeatherData(row.id, sensor.id, msg.payload)
        insertWeatherData(connection, weatherData)
        updateRowDefaultConf(connection, weatherData)

        print("\n")
        print("MQTT Data Received...")
        print("MQTT Topic: " + newTopic[0])
        print("MQTT Sensor: " + newTopic[1])
        print("Data: " + str(msg.payload))
        print("\n")

        if sensor.id == 3:
            if int(weatherData.getValue()) == 0:
                setEnvStatus(environment, "off")
            else:
                setEnvStatus(environment, "on")

        if sensor.id == 2:
            # Valor referente do dimmer lido em cada linha/ambiente
            value = "{:.1f}".format(float(weatherData.getValue()))

            # Realiza uma regra de 3 para verificar o valor correspondente
            # em volts (aproximadamente)
            volts = (float(float(value)) * 220) / 95
            # Sensor correspondente ao tensao eletrica
            s_volts = 4
            # Cria o WeatherData e armazena no banco
            wd_volts = WeatherData(row.id, s_volts, volts)
            insertWeatherData(connection, wd_volts)

            # Valor de amperagem dos cabos
            amps = (float(float(value)) * 1.6) / 95
            # Sensor correspondente ao corrente eletrica
            s_amps = 5
            # Cria o WeatherData e armazena no banco
            wd_amps = WeatherData(row.id, s_amps, amps)
            insertWeatherData(connection, wd_amps)

        # Calculo do consumo em watts
        watts = float(volts * amps)
        # Sensor correspondente ao consumo
        s = 7
        # Cria o WeatherData e armazena no banco
        wd = WeatherData(row.id, s, watts)
        insertWeatherData(connection, wd)
Пример #20
0
class Weather2DBImport:
    """
    This class contains logic to populate the weather database with
    GBT weather data.
    """
    def __init__(self, dbname=""):
        self.c = pg.connect(user="******",
                            dbname=dbname,
                            port=settings.DATABASE_PORT)
        self.weatherData = WeatherData()
        self.pyrgeometerData = PyrgeometerData()

    def getNeededWeatherDates(self, dt=None):
        """
        Get's those dates that don't have any accompanying weather data.
        """
        if dt is None:
            dt = datetime.utcnow()
        r = \
            self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM gbt_weather)
                               AND date <= '%s'
                         """ % dt)
        return [(row['id'], row['date']) for row in r.dictresult()]

    def getNeededWeatherDatesInRange(self, start, end):
        """
        Get's those dates that don't have any accompanying weather data.
        """
        r = self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM gbt_weather)
                               AND date >= '%s'
                               AND date <  '%s'
                         """ % (start, end))
        return [(row['id'], row['date']) for row in r.dictresult()]

    def insert(self, weatherDateId, wind, irradiance):
        """
        Inserts a row of data into the weather table.
        """
        # handle missing data - put in what you can
        windOK = wind and wind == wind
        irradianceOK = irradiance and irradiance == irradiance
        if windOK and irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,wind_speed,irradiance)
                    VALUES (%s, %s, %s)
                    """ % (weatherDateId, wind, irradiance)
        elif windOK and not irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,wind_speed)
                    VALUES (%s, %s)
                    """ % (weatherDateId, wind)
        elif not windOK and irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,irradiance)
                    VALUES (%s, %s)
                    """ % (weatherDateId, irradiance)
        if windOK or irradianceOK:
            self.c.query(query)

    def update(self):
        """
        Looks to see what weather times need updating, then retrieves that
        data from the sampler logs, and finally writes results to DB.
        """

        results = []

        # look for any missing data within the last year
        end = datetime.utcnow()
        start = end - timedelta(days=365)
        dts = self.getNeededWeatherDatesInRange(start, end)

        for dtId, dtStr in dts:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            try:
                wind = self.weatherData.getHourDanaMedianSpeeds(dt)
            except:
                continue
            di = self.pyrgeometerData.getHourMedianDownwardIrradiance(dt)
            results.append((dtId, wind, di))
            self.insert(dtId, wind, di)
        return results

    def findNullValues(self, column):
        "Who is missing a value?"
        query = """
                SELECT gbt.id, wd.date 
                FROM gbt_weather AS gbt, weather_dates AS wd
                WHERE gbt.%s is NULL AND gbt.weather_date_id = wd.id
                """ % column
        r = self.c.query(query)
        return [(row['id'], row['date']) for row in r.dictresult()]

    def updateRow(self, rowId, column, value):
        """
        Updates a row in the weather table with a value.
        """
        query = """
                UPDATE gbt_weather SET %s = %s WHERE id = %d
                """ % (column, value, rowId)
        self.c.query(query)

    def backfill(self, column, callback, test=False):
        """
        Generic method for looking for null values in the weather table,
        and updating those rows with the appropriate value from the 
        sampler logs.
        """

        results = []
        missing = self.findNullValues(column)
        for id, dtStr in missing:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            v = callback(dt)
            # watch for NaN values
            if v and v == v:
                results.append((id, dtStr, v))
                if not test:
                    self.updateRow(id, column, v)
        return results

    def backfillWind(self, test=False):
        return self.backfill("wind_speed",
                             self.weatherData.getLastHourMedianWindSpeeds,
                             test)

    def backfillIrradiance(self):
        return self.backfill(
            "irradiance",
            self.pyrgeometerData.getLastHourMedianDownwardIrradiance)

    def backfillReport(self, filename):
        "Backfills the DB, and creates report on results."

        # NOTE: current results for this method: using the weather or
        # or weather_unit_test DB's, since we only have 2006 & 2009 - present
        # data in those, and there is no 2006 pygeometer data, this method
        # only backfills in 2009 - present

        f = open(filename, 'w')
        # wind
        lines = []
        lines.append("Wind Speed\n")
        lines.append("Start (ET): %s\n" % datetime.now())
        results = self.backfillWind()
        for r in results:
            lines.append("%s,%s,%s\n" % (r[0], r[1], r[2]))
        lines.append("End (ET): %s\n" % datetime.now())
        f.writelines(lines)
        # irradiance
        lines = []
        lines.append("Irradiance\n")
        lines.append("Start (ET): %s\n" % datetime.now())
        results = self.backfillIrradiance()
        for r in results:
            lines.append("%s,%s,%s\n" % (r[0], r[1], r[2]))
        lines.append("End (ET): %s\n" % datetime.now())
        f.writelines(lines)
        f.close()
        print "printed report to: ", filename

    def backfillDatabase(self, starttime, endtime):
        """
        Acquires the needed data whose dates that don't have any
        accompanying weather data, and inserts it into the database.
        """
        dts = self.getNeededWeatherDatesInRange(starttime, endtime)
        for dtId, dtStr in dts:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            # Is there wind data?
            try:
                wind = self.weatherData.getHourDanaMedianSpeeds(dt)
            except:
                continue
            di = self.pyrgeometerData.getHourMedianDownwardIrradiance(dt)
            # Is irradiance a NaN?
            if di != di:
                di = None
            print dt, wind, di
            self.insert(dtId, wind, di)
Пример #21
0
        "13n": "snow",
        "50d": "mist",
        "50n": "mist"
    }

    weekdays = {0: "Mo", 1: "Di", 2: "Mi", 3: "Do", 4: "Fr", 5: "Sa", 6: "So"}

    while (1):

        # Drawing on the Horizontal image
        Limage = Image.new('1', (epd.height, epd.width),
                           255)  # 255: clear the frame
        draw = ImageDraw.Draw(Limage)

        #get current weather data
        weatherdata = WeatherData('49.591', '8.646', 'apitoken')
        currentWeather = weatherdata.getCurrentWeather()

        #date
        today = date.today()
        draw.text(
            (20, 10),
            "Hemsbach         " + weekdays[today.weekday()] + " " +
            str(today.day) + "." + str(today.month) + "." + str(today.year),
            font=font32b,
            fill=0)

        #icon
        logging.info("read icon bmp file")
        iconbmp = Image.open(
            os.path.join(iconsdir, icons[currentWeather.currentIcon] + '.bmp'))
Пример #22
0
from WeatherData import WeatherData
from Observer import CurrentConditionsDisplay, Heatindex

s = WeatherData()
o = CurrentConditionsDisplay(s)
h = Heatindex(s)
s.setMeasurements(25, 30.1, 43)
s.setMeasurements(31, 42, 54)
Пример #23
0
 def __init__(self, year):
     self.dtFormat = "%Y-%m-%d %H:%M:%S"
     self.weatherData = WeatherData()
     self.start = datetime(year, 1, 1, 0, 0, 0)
     self.end = self.start + timedelta(hours = 24*365)
Пример #24
0
 def __init__(self, dbname="weather"):
     self.c = pg.connect(user="******", dbname=dbname)
     self.weatherData = WeatherData()
  def doUpload(self,**params):
    count = cherrypy.session.get("count", 0) + 1
    cherrypy.session["count"] = count
    sess = cherrypy.session
    sId = sess._id
    
    # run form submission validation
    errs = self.validate(params)
    if(len(errs) != 0):
      template = env.get_template("upload.html")
      return template.render( { "errs"        : errs, 
                                "params"      : params,
                                "formOptions" : self.formOptions } )
    
    # add the form data to the session so it is accessible until the end of the session
    sess.update(params)
    userDir = getUserDir()
    if not os.path.exists(userDir): os.makedirs(userDir) # create the directories as needed
    upFile = params.get("upFile",None)
    ext = upFile.filename.split(".")[-1]
    rawFilePath = os.path.join(userDir,"raw_GB_upload.%s" % (ext))
    size = 0
    with open(rawFilePath,"wb") as rawFile:
      while True:
        data = upFile.file.read(8192)
        if not data: break
        size += len(data)
        rawFile.write(data)
    print "uploaded file to: ", rawFilePath
    parseTargetFile = getDataFile(ext=ext)
    try:
      if(zipfile.is_zipfile(rawFilePath)): # try to find the electric interval data inside with a couple of heuristics
        # note that PGE's format looks like this "pge_electric_interval_data_2013-02-01_to_2013-04-28.xml"
        # note that SDGE's format looks like this "SDGE_Electric_15_Minute_06-30-2012_07-30-2013_20130801103424053.xml"
        matchPatterns = (".*electric.*\.xml",".*electric.*\.csv") # only PGE & SDGE for now. Could add more.
        zf = zipfile.ZipFile(rawFilePath,"r")
        matched = None
        for innerFile in zf.namelist():
          print(innerFile)
          for pattern in matchPatterns:
            match = re.match(pattern,innerFile,re.I) # note "re.match" searches from the beginning of the string, "re.search" scans. re.I makes the match case insensitive.
            if match is not None: 
              matched = match
              ext = match.string.split(".")[-1]
              parseTargetFile = getDataFile(ext=ext)
              with open(parseTargetFile,"wb") as target:
                source = zf.open(innerFile,"r")
                try: shutil.copyfileobj(source, target)
                finally: source.close()
              break
        if matched is None: raise Exception("""Unrecognized zip format. There are many providers of Smart Meter data,
                                   and they use whatever file naming convention they want so we don't always
                                   know what file to look for inside the zip file. Please report this via
                                   the feedback page, and try again with your xml file unzipped.""")
      else: # if(ext == "xml"):
        try: 
          os.remove(parseTargetFile) # in case a file was already uploaded during this session, get it out of the way
                                     # rename won"t overwrite an existing file on Windows... see os.rename
        except: pass # if it doesn"t exist, no problem. If it is currently in use, then next line will fail.
        os.rename(rawFilePath,parseTargetFile)
      #else: raise Exception("Unrecognized file type %s" % ext)

      parsedData = analysis.parseDataFile(parseTargetFile)

      dates = parsedData.getReadings()[0]
      dateDiff = [j-i for i, j in zip(dates[:-1], dates[1:])]
      #print dateDiff
      if(min(dateDiff) > datetime.timedelta(hours=1)): raise Exception("Time difference must be at most 1 hour between readings")
    
    #except xml.parsers.expat.ExpatError as ee:
    #  print ee
    #  errs["upFile"] = "Your file does not appear to be in a Green Button XML format. Please check the file and try again."
    except Exception as e: 
      print e
      errs["upFile"] = str(e)

    if(len(errs) != 0):
      template = env.get_template("upload.html")
      return template.render( { "errs"        : errs, 
                                "params"      : params,
                                "formOptions" : self.formOptions } )

    params["filename"]    = upFile.filename
    params["filesize"]    = size
    params["filetype"]    = str(upFile.content_type)
    params["upload_time"] = datetime.datetime.now()
    del params["upFile"] # remove the upload file obj
    # write params to working dir so session can be recreased
    with open(os.path.join(userDir,'params.pkl'),'wb') as paramFile:
      pickle.dump(params,paramFile)
    #with open(os.path.join(userDir,'params.pkl'),'rb') as paramFile:
    #  print pickle.load(paramFile)
    bldg = Building(parsedData.getReadings(),params['bldg_zip'],params)
    cherrypy.session["building"] = bldg
    weather = WeatherData("weather")
    first = bldg.days[0]
    last = bldg.days[-1]
    # warning. This can trigger a long download that is not in a separate thread
    bestWBAN = weather.closestWBAN(int(params["bldg_zip"]),first.year,first.month)
    sess["bestWBAN"]      = bestWBAN
    bldg.attr["bestWBAN"] = bestWBAN

    pm = PlotMaker(bldg,getUserDir(),cherrypy.config.get("wkhtmltopdf.bin"),sId)
    threading.Thread(target=pm.generateFiles).start()
    time.sleep(0.25) # let the lock file get written
    sess["filename"] = upFile.filename
    sess["filesize"] = size
    sess["filetype"] = upFile.content_type
    sess["filepath"] = parseTargetFile
    template = env.get_template("dataExplore.html")
    response_dict = {}
    response_dict.update(sess)
    return self.explore()
Пример #26
0
 def __init__(self, dbname=""):
     self.c = pg.connect(user="******",
                         dbname=dbname,
                         port=settings.DATABASE_PORT)
     self.weatherData = WeatherData()
     self.pyrgeometerData = PyrgeometerData()
Пример #27
0
class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle("Smart mirror")
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.black)
        self.degree = unichr(176)
        self.redrawCount = 0
        self.calendarRefreshCount = 0
        self.setPalette(p)

        self.prevTrainLabel = QLabel(self)
        self.prevTrainLabel1 = QLabel(self)
        self.prevTrainLabel2 = QLabel(self)
        self.prevTrainLabel3 = QLabel(self)

        self.nextTrainLabel = QLabel(self)
        self.nextTrainLabel1 = QLabel(self)
        self.nextTrainLabel2 = QLabel(self)
        self.nextTrainLabel3 = QLabel(self)

        self.nextTrain1Label = QLabel(self)
        self.nextTrain1Label1 = QLabel(self)
        self.nextTrain1Label2 = QLabel(self)
        self.nextTrain1Label3 = QLabel(self)

        self.nextTrain2Label = QLabel(self)
        self.nextTrain2Label1 = QLabel(self)
        self.nextTrain2Label2 = QLabel(self)
        self.nextTrain2Label3 = QLabel(self)

        self.startTime = QLabel(self)
        self.lateTime = QLabel(self)
        self.arrivalTime = QLabel(self)
        self.arrivalStation = QLabel(self)
        self.line = QLabel(self)
        self.time = QLabel(self)
        self.date = QLabel(self)

        self.temperature = QLabel(self)
        self.weather = [QLabel(self), QLabel(self), QLabel(self)]
        self.clouds = QLabel(self)
        self.wind = QLabel(self)

        self.weatherForecastWeek = []
        self.forecastSeparatorLine = []
        self.forecastSeparatorPartOfTheDay = []
        self.forecastDayIdentify = []
        for i in range(4):
            self.forecastSeparatorLine.append(QLabel(self))
            self.forecastSeparatorPartOfTheDay.append(QLabel(self))
        for i in range(5):
            self.forecastDayIdentify.append(QLabel(self))
        for i in range(5 * forecastRows):
            self.weatherForecastWeek.append(QLabel(self))

        self.calendarEntries = []

        for i in range(15):
            self.calendarEntries.append(QLabel(self))

        self.trainLabelFont = QtGui.QFont("Times", 35, QtGui.QFont.Normal)
        self.dayLabelFont = QtGui.QFont("Times", 30, QtGui.QFont.Normal)
        self.timeLabelFont = QtGui.QFont("Times", 150, QtGui.QFont.Normal)
        self.tempLabelFont = QtGui.QFont("Times", 100, QtGui.QFont.Normal)
        self.dateLabelFont = QtGui.QFont("Times", 45, QtGui.QFont.Normal)
        self.forecastLabelFont = QtGui.QFont("Times", 20, QtGui.QFont.Normal)
        self.forecastLabelFontBold = QtGui.QFont("Times", 20, QtGui.QFont.Bold)

        exit_action = QAction("", self)
        exit_action.setShortcut("Ctrl+Q")
        exit_action.setStatusTip('')
        exit_action.triggered.connect(self.showNormalAndExit)
        exit_full_screen = QAction("", self)
        exit_full_screen.setShortcut("Ctrl+X")
        exit_full_screen.setStatusTip('')
        exit_full_screen.triggered.connect(self.showNormal)
        return_full_screen = QAction("", self)
        return_full_screen.setShortcut("Ctrl+A")
        return_full_screen.setStatusTip('')
        return_full_screen.triggered.connect(self.showFullScreen)

        self.colorWhite = 'color: white'
        self.colorGrey = 'color: grey'
        self.colorRed = 'color: red'
        self.colorDarkGrey = 'color: #1e1e1e'

        self.statusBar()
        self.setStyleSheet("""
            QMenuBar {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
                border: 1px solid #000;
            }

            QMenuBar::item {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
            }

            QMenuBar::item::selected {
                background-color: rgb(0,0,0);
            }

            QMenu {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
                border: 1px solid #000;           
            }

            QMenu::item::selected {
                background-color: rgb(0,0,0);
            }
        """)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('')
        fileMenu.addAction(exit_action)
        fileMenu.addAction(exit_full_screen)
        fileMenu.addAction(return_full_screen)
        mainMenu.resize(0, 0)

        self.showFullScreen()
        self.trainData = TrainData()
        self.weatherData = WeatherData()
        self.setTrainTexts()
        self.initTrainsLabel()
        self.initDateTimeLabels()
        self.initWeatherLabels()
        self.initForecastWeatherLabels()
        self.initCalendarLabels()
        self.setTrains()
        self.setDateTime()
        self.setWeather()
        self.setForecastWeather()
        self.setCalendar()
        # sensor = Process()
        # subprocess.run('python3 sensor_handler.py', shell=True, start_new_session=True)

    def initTrainsLabel(self):
        self.prevTrainLabel.setStyleSheet(self.colorWhite)
        self.prevTrainLabel.move(trainsXOffset, firstRowYChord + rowOffset)
        self.prevTrainLabel.setFont(self.trainLabelFont)
        self.prevTrainLabel.resize(trainLabelWidth, 40)
        self.prevTrainLabel1.move(trainsXOffset + cellOffset, firstRowYChord + rowOffset)
        self.prevTrainLabel1.setStyleSheet(self.colorWhite)
        self.prevTrainLabel1.setFont(self.trainLabelFont)
        self.prevTrainLabel.resize(trainLabelWidth, 40)
        self.prevTrainLabel2.move(trainsXOffset + 2 * cellOffset, firstRowYChord + rowOffset)
        self.prevTrainLabel2.setStyleSheet(self.colorWhite)
        self.prevTrainLabel2.setFont(self.trainLabelFont)
        self.prevTrainLabel2.resize(trainLabelWidth, 40)
        self.prevTrainLabel3.move(trainsXOffset + 3 * cellOffset, firstRowYChord + rowOffset)
        self.prevTrainLabel3.setStyleSheet(self.colorWhite)
        self.prevTrainLabel3.setFont(self.trainLabelFont)
        self.prevTrainLabel3.resize(trainLabelWidth, 40)

        self.nextTrainLabel.move(trainsXOffset, firstRowYChord + 2 * rowOffset)
        self.nextTrainLabel.setStyleSheet(self.colorWhite)
        self.nextTrainLabel.setFont(self.trainLabelFont)
        self.nextTrainLabel.resize(trainLabelWidth, 40)
        self.nextTrainLabel1.move(trainsXOffset + cellOffset, firstRowYChord + 2 * rowOffset)
        self.nextTrainLabel1.setStyleSheet(self.colorWhite)
        self.nextTrainLabel1.setFont(self.trainLabelFont)
        self.nextTrainLabel1.resize(trainLabelWidth, 40)
        self.nextTrainLabel2.move(trainsXOffset + 2 * cellOffset, firstRowYChord + 2 * rowOffset)
        self.nextTrainLabel2.setStyleSheet(self.colorWhite)
        self.nextTrainLabel2.setFont(self.trainLabelFont)
        self.nextTrainLabel2.resize(trainLabelWidth, 40)
        self.nextTrainLabel3.move(trainsXOffset + 3 * cellOffset, firstRowYChord + 2 * rowOffset)
        self.nextTrainLabel3.setStyleSheet(self.colorWhite)
        self.nextTrainLabel3.setFont(self.trainLabelFont)
        self.nextTrainLabel3.resize(trainLabelWidth, 40)

        self.nextTrain1Label.move(trainsXOffset, firstRowYChord + 3 * rowOffset)
        self.nextTrain1Label.setStyleSheet(self.colorGrey)
        self.nextTrain1Label.setFont(self.trainLabelFont)
        self.nextTrain1Label.resize(trainLabelWidth, 40)
        self.nextTrain1Label1.move(trainsXOffset + cellOffset, firstRowYChord + 3 * rowOffset)
        self.nextTrain1Label1.setStyleSheet(self.colorGrey)
        self.nextTrain1Label1.setFont(self.trainLabelFont)
        self.nextTrain1Label1.resize(trainLabelWidth, 40)
        self.nextTrain1Label2.move(trainsXOffset + 2 * cellOffset, firstRowYChord + 3 * rowOffset)
        self.nextTrain1Label2.setStyleSheet(self.colorGrey)
        self.nextTrain1Label2.setFont(self.trainLabelFont)
        self.nextTrain1Label2.resize(trainLabelWidth, 40)
        self.nextTrain1Label3.move(trainsXOffset + 3 * cellOffset, firstRowYChord + 3 * rowOffset)
        self.nextTrain1Label3.setStyleSheet(self.colorGrey)
        self.nextTrain1Label3.setFont(self.trainLabelFont)
        self.nextTrain1Label3.resize(trainLabelWidth, 40)

        self.nextTrain2Label.move(trainsXOffset, firstRowYChord + 4 * rowOffset)
        self.nextTrain2Label.setStyleSheet(self.colorDarkGrey)
        self.nextTrain2Label.setFont(self.trainLabelFont)
        self.nextTrain2Label.resize(trainLabelWidth, 40)
        self.nextTrain2Label1.move(trainsXOffset + cellOffset, firstRowYChord + 4 * rowOffset)
        self.nextTrain2Label1.setStyleSheet(self.colorDarkGrey)
        self.nextTrain2Label1.setFont(self.trainLabelFont)
        self.nextTrain2Label1.resize(trainLabelWidth, 40)
        self.nextTrain2Label2.move(trainsXOffset + 2 * cellOffset, firstRowYChord + 4 * rowOffset)
        self.nextTrain2Label2.setStyleSheet(self.colorDarkGrey)
        self.nextTrain2Label2.setFont(self.trainLabelFont)
        self.nextTrain2Label2.resize(trainLabelWidth, 40)
        self.nextTrain2Label3.move(trainsXOffset + 3 * cellOffset, firstRowYChord + 4 * rowOffset)
        self.nextTrain2Label3.setStyleSheet(self.colorDarkGrey)
        self.nextTrain2Label3.setFont(self.trainLabelFont)
        self.nextTrain2Label3.resize(trainLabelWidth, 40)

    def setTrains(self):
        try:
            self.trainData.refreshTrainList()
        except:
            self.allTrainInfo = []
            print('Exception during train data request')
            traceback.print_exc()

        if self.trainData.getPrevTrain() is not None:
            # Cleat label content
            self.prevTrainLabel.setText("")
            self.prevTrainLabel1.setText("")
            self.prevTrainLabel2.setText("")
            self.prevTrainLabel3.setText("")

            self.prevTrainLabel.setText(str(self.trainData.getPrevTrain()[INDEXES["Indulás"]]["Indulás"]))
            self.prevTrainLabel1.setText(str(self.trainData.getPrevTrain()[INDEXES["Késés"]]["Késés"]))
            self.prevTrainLabel2.setText(str(self.trainData.getPrevTrain()[INDEXES["Érkezés"]]["Érkezés"]))
            self.prevTrainLabel3.setText(str(self.trainData.getPrevTrain()[INDEXES["Végállomás"]]["Végállomás"]))
        else:
            self.prevTrainLabel.setText("")
            self.prevTrainLabel1.setText("")
            self.prevTrainLabel2.setText("")
            self.prevTrainLabel3.setText("")

        if self.trainData.getNextTrains()[0] is not None:
            # Cleat label content
            self.nextTrainLabel.setText("")
            self.nextTrainLabel1.setText("")
            self.nextTrainLabel2.setText("")
            self.nextTrainLabel3.setText("")

            self.nextTrainLabel.setText(str(self.trainData.getNextTrains()[0][INDEXES["Indulás"]]["Indulás"]))
            self.nextTrainLabel1.setText(str(self.trainData.getNextTrains()[0][INDEXES["Késés"]]["Késés"]))
            self.nextTrainLabel2.setText(str(self.trainData.getNextTrains()[0][INDEXES["Érkezés"]]["Érkezés"]))
            self.nextTrainLabel3.setText(str(self.trainData.getNextTrains()[0][INDEXES["Végállomás"]]["Végállomás"]))
        else:
            self.nextTrainLabel.setText("")
            self.nextTrainLabel1.setText("")
            self.nextTrainLabel2.setText("")
            self.nextTrainLabel3.setText("")

        if self.trainData.getNextTrains()[1] is not None:
            # Cleat label content
            self.nextTrain1Label.setText("")
            self.nextTrain1Label1.setText("")
            self.nextTrain1Label2.setText("")
            self.nextTrain1Label3.setText("")

            self.nextTrain1Label.setText(str(self.trainData.getNextTrains()[1][INDEXES["Indulás"]]["Indulás"]))
            self.nextTrain1Label1.setText(str(self.trainData.getNextTrains()[1][INDEXES["Késés"]]["Késés"]))
            self.nextTrain1Label2.setText(str(self.trainData.getNextTrains()[1][INDEXES["Érkezés"]]["Érkezés"]))
            self.nextTrain1Label3.setText(str(self.trainData.getNextTrains()[1][INDEXES["Végállomás"]]["Végállomás"]))
        else:
            self.nextTrain1Label.setText("")
            self.nextTrain1Label1.setText("")
            self.nextTrain1Label2.setText("")
            self.nextTrain1Label3.setText("")

        if self.trainData.getNextTrains()[2] is not None:
            # Cleat label content
            self.nextTrain2Label.setText("")
            self.nextTrain2Label1.setText("")
            self.nextTrain2Label2.setText("")
            self.nextTrain2Label3.setText("")

            self.nextTrain2Label.setText(str(self.trainData.getNextTrains()[2][INDEXES["Indulás"]]["Indulás"]))
            self.nextTrain2Label1.setText(str(self.trainData.getNextTrains()[2][INDEXES["Késés"]]["Késés"]))
            self.nextTrain2Label2.setText(str(self.trainData.getNextTrains()[2][INDEXES["Érkezés"]]["Érkezés"]))
            self.nextTrain2Label3.setText(str(self.trainData.getNextTrains()[2][INDEXES["Végállomás"]]["Végállomás"]))
        else:
            self.nextTrain2Label.setText("")
            self.nextTrain2Label1.setText("")
            self.nextTrain2Label2.setText("")
            self.nextTrain2Label3.setText("")

    def setTrainTexts(self):
        self.startTime.setText("Indulás")
        self.startTime.setStyleSheet(self.colorWhite)
        self.startTime.setFont(self.trainLabelFont)
        self.startTime.move(trainsXOffset, firstRowYChord)
        self.startTime.resize(150, 50)
        self.lateTime.setText("Késés")
        self.lateTime.setStyleSheet(self.colorWhite)
        self.lateTime.setFont(self.trainLabelFont)
        self.lateTime.move(trainsXOffset + cellOffset, firstRowYChord)
        self.lateTime.resize(150, 50)
        self.arrivalTime.setText("Érkezés")
        self.arrivalTime.setStyleSheet(self.colorWhite)
        self.arrivalTime.setFont(self.trainLabelFont)
        self.arrivalTime.move(trainsXOffset + 2 * cellOffset, firstRowYChord)
        self.arrivalTime.resize(150, 50)
        self.arrivalStation.setText("Végállomás")
        self.arrivalStation.setStyleSheet(self.colorWhite)
        self.arrivalStation.setFont(self.trainLabelFont)
        self.arrivalStation.resize(220, 50)
        self.arrivalStation.move(trainsXOffset + 3 * cellOffset, firstRowYChord)
        self.line.setText("------------------------------------------------------------")
        self.line.move(trainsXOffset, firstRowYChord + rowOffset + 20)
        self.line.setStyleSheet(self.colorWhite)
        self.line.setFont(self.trainLabelFont)
        self.line.resize(1000, 50)

    def initDateTimeLabels(self):
        self.time.move(20, 40)
        self.time.setStyleSheet(self.colorWhite)
        self.time.setFont(self.timeLabelFont)
        self.time.resize(550, 150)
        self.date.move(20, 350)
        self.date.setStyleSheet(self.colorWhite)
        self.date.setFont(self.dateLabelFont)
        self.date.resize(550, 65)

    def setDateTime(self):
        now = datetime.datetime.now()
        toDayWeekDay = datetime.datetime.today().weekday()
        minute = now.minute

        if minute < 10:
            minute = '0' + str(minute)

        month = now.month
        if month < 10:
            month = '0' + str(month)
        day = now.day
        if day < 10:
            day = '0' + str(day)
        # print(now.hour, ":", now.minute)
        self.date.setText(str(now.year) + '.' + str(month) + '.' + str(day) + '. ' + weekDayList[toDayWeekDay])
        self.time.setText(str(now.hour) + ' : ' + str(minute))

    def initCalendarLabels(self):
        j = 0
        for i in self.calendarEntries:
            i.setStyleSheet(self.colorWhite)
            i.setFont(self.forecastLabelFont)
            i.resize(180, 100)
            if j <= 4:  # First row for the event name
                i.move(20 + j * calendarCellOffset, calendarYstartPosition)
            elif j <= 9:  # Second row for the event date
                i.move(20 + ((j - 5) * calendarCellOffset), calendarYstartPosition + calendarRowOffet)
            else:
                i.move(20 + ((j - 10) * calendarCellOffset), calendarYstartPosition + calendarRowOffet * 2)
            j += 1

    def setCalendar(self):
        events = googleAPI.getEvents()
        eventList = []
        for event in events:
            eventDict = {}
            eventDict['today'] = False
            start = event['start'].get('dateTime', event['start'].get('date'))
            end = event['end'].get('dateTime', event['end'].get('date'))
            start_1 = start.split('T')
            end_1 = end.split('T')
            eventDict['event'] = str(event['summary'])
            if (time.mktime(time.strptime(end_1[0], '%Y-%m-%d')) - time.mktime(time.strptime(start_1[0], '%Y-%m-%d'))) / 3600 > 24: # the time is more than 24 h
                if ((time.mktime(time.localtime()) - time.mktime(time.strptime(start_1[0], '%Y-%m-%d'))) / 3600 == 0.0):
                    eventDict['today'] = True
                eventDict['date'] = (start_1[0] + ' tól')
                eventDict['date_2'] = (end_1[0] + ' ig')
            else:
                if (abs((time.mktime(time.localtime()) - time.mktime(
                        time.strptime(start_1[0], '%Y-%m-%d'))) / 3600) < 24):
                    eventDict['today'] = True
                eventDict['date'] = start_1[0]
                try:
                    eventDict['time'] = start_1[1]
                except:
                    eventDict['time'] = ''
                    print('No time specified for this event.')
            eventList.append(eventDict)
        j = 0
        for i in self.calendarEntries:
            if j <= 4:
                i.setText(eventList[j]['event'])
                if eventList[j-5]['today']:
                    i.setFont(self.forecastLabelFontBold)
                else:
                    i.setFont(self.forecastLabelFont)
            elif j <= 9:
                i.setText(eventList[j-5]['date'])
                if eventList[j-5]['today']:
                    i.setFont(self.forecastLabelFontBold)
                else:
                    i.setFont(self.forecastLabelFont)
            else:
                try:
                i.setText(eventList[j-10]['time'])
                except KeyError:  # More days, time is not specified
                    i.setText(eventList[j-10]['date_2'])
                if eventList[j-10]['today']:
                    i.setFont(self.forecastLabelFontBold)
                else:
                    i.setFont(self.forecastLabelFont)
            j += 1


    def initWeatherLabels(self):
        self.temperature.setStyleSheet(self.colorWhite)
        self.temperature.move(temperatureXOffset, 40)
        self.temperature.setFont(self.tempLabelFont)
        self.temperature.resize(350, 150)
        for i in range(0, 3):
            self.weather[i].setStyleSheet(self.colorWhite)
            self.weather[i].move(temperatureXOffset, 190 + i * rowOffset)
            self.weather[i].setFont(self.trainLabelFont)
            self.weather[i].resize(350, 50)
        self.clouds.setStyleSheet(self.colorWhite)
        self.clouds.move(temperatureXOffset, 190 + 3 * rowOffset)
        self.clouds.setFont(self.trainLabelFont)
        self.clouds.resize(350, 50)
        self.wind.setStyleSheet(self.colorWhite)
        self.wind.move(temperatureXOffset, 190 + 4 * rowOffset)
        self.wind.setFont(self.trainLabelFont)
        self.wind.resize(350, 50)

    def initForecastWeatherLabels(self):
        count = 0
        multi = 0
        for i in self.weatherForecastWeek:
            i.setStyleSheet(self.colorWhite)
            i.setFont(self.forecastLabelFont)
            i.resize(400, 65)
            if count % forecastRows == 0:
                multi = count / forecastRows
            i.move(100 + multi * fCellOffset,
                   firstRowYChord + forecastWeatherTableYOffset + weatherForecastrowOffsetMultiplier * rowOffset + (
                           count % forecastRows) * forecastRowOffset)
            count += 1
        partOfTheDay = ["Reggel", "Nappal", "Este", " "]
        for i in range(4):
            self.forecastSeparatorLine[i].setText(
                "------------------------------------------------------")
            self.forecastSeparatorLine[i].move(100, firstRowYChord + forecastWeatherTableYOffset +
                                               weatherForecastrowOffsetMultiplier * rowOffset + (
                                                       i * ((forecastRows / 3) * forecastRowOffset)) - 5)
            self.forecastSeparatorLine[i].setStyleSheet(self.colorWhite)
            self.forecastSeparatorLine[i].setFont(self.trainLabelFont)
            self.forecastSeparatorLine[i].resize(1000, 35)

            self.forecastSeparatorPartOfTheDay[i].setText(partOfTheDay[i])
            self.forecastSeparatorPartOfTheDay[i].move(5, firstRowYChord + 50 + forecastWeatherTableYOffset +
                                                       weatherForecastrowOffsetMultiplier * rowOffset + (
                                                               i * ((forecastRows / 3) * forecastRowOffset)))
            self.forecastSeparatorPartOfTheDay[i].setStyleSheet(self.colorWhite)
            self.forecastSeparatorPartOfTheDay[i].setFont(self.forecastLabelFont)
            self.forecastSeparatorPartOfTheDay[i].resize(100, 40)

        for i in range(5):
            self.forecastDayIdentify[i].move(100 + i * fCellOffset,
                                             firstRowYChord + forecastWeatherTableYOffset + weatherForecastrowOffsetMultiplier * rowOffset - 30)
            self.forecastDayIdentify[i].setStyleSheet(self.colorWhite)
            self.forecastDayIdentify[i].setFont(self.dayLabelFont)
            self.forecastDayIdentify[i].resize(1000, 35)

    def setWeather(self):
        weatherData = self.weatherData.refreshActualWeatherData()
        self.temperature.setText(str(round(weatherData['main']['temp'])) + self.degree + 'C')
        a = 0
        for weather in weatherData['weather']:
            self.weather[a].setText(weather['description'])
            a += 1
        for i in range(a, 3):
            self.weather[a].setText('')
        self.clouds.setText("Felhõzet: " + str(weatherData['clouds']['all']) + '%')
        self.wind.setText("Szél: " + str(weatherData['wind']['speed']) + " m/s")

    def setForecastWeather(self):
        forecastWeatherData = self.weatherData.prepareDayliForecast(self.weatherData.refreshForecastWeatherData())
        toDayWeekDay = datetime.datetime.today().weekday()
        count = 0
        for i in forecastWeatherData:
            if i != {}:
                self.weatherForecastWeek[count].setText(
                    str(round(i['temp_min'])) + "  -  " + str(round(i['temp_max'])) + self.degree + 'C')
            count += partOfTheDayLines
        count = 1
        for i in forecastWeatherData:
            if i != {}:
                for j in range(len(i['weather'])):
                    self.weatherForecastWeek[count + j].setText(str(i['weather'][j]))
            count += partOfTheDayLines
        for i in range(5):
            weekDayListIndex = toDayWeekDay + i
            if weekDayListIndex > 6:
                weekDayListIndex = weekDayListIndex - 7
            self.forecastDayIdentify[i].setText(str(weekDayList[weekDayListIndex]))
        '''count = 4
        for i in forecastWeatherData:
            if i != {}:
                try:
                    self.weatherForecastWeek[count].setText(str(round(i['snow'], 2)))
                except TypeError:
                    self.weatherForecastWeek[count].setText(str(i['snow']))
            count += increment

        count = 5
        for i in forecastWeatherData:
            if i != {}:
                try:
                    self.weatherForecastWeek[count].setText(str(round(i['rain'], 2)))
                except TypeError:
                    self.weatherForecastWeek[count].setText(str(i['rain']))
            count += increment
            '''

    def updateTrainsAndDate(self):
        # self.cleanTrains()
        self.setDateTime()
        self.setTrains()

    def setwindowSize(self):
        self.showNormal()

    @pyqtSlot()
    def exiting(self):
        print("Exit")
        self.t.cancel()
        sys.exit()

    def showNormalAndExit(self):
        self.showNormal()
        self.exiting()

    def startRefreshThread(self):
        try:
            self.setDateTime()
        except:
            print('setDateTime exception')
            traceback.print_exc()
        try:
            self.setTrains()
        except:
            print('setTrains exception')
            traceback.print_exc()
        try:
            self.setWeather()
        except:
            print('setWeather exception')
            traceback.print_exc()
        try:
            self.setForecastWeather()
        except:
            print('setForecastWeather exception')
            traceback.print_exc()

        self.redrawCount += 1
        console = subprocess.check_output('tvservice -s', shell=True).split()[1]
        print(subprocess.check_output('tvservice -s', shell=True).split()[1])
#         if console == b'0x120002' and self.redrawCount > 10:  # For redraw purpose
        if console == b'0x2' and self.redrawCount > 10:  # For redraw purpose
            self.showNormal()
            self.showFullScreen()
            self.redrawCount = 0
            
        self.calendarRefreshCount += 1
        if self.calendarRefreshCount > 720: # 4 times a day
            self.setCalendar()
            self.calendarRefreshCount = 0

        self.t = threading.Timer(30, self.startRefreshThread)
        self.t.start()
Пример #28
0
from WeatherData import WeatherData
from DisplayElements import *

weatherData = WeatherData()

currentConditionDisplay = CurrentConditionDisplay(weatherData)
statisticsDisplay = StatisticsDisplay(weatherData)

weatherData.setMeasurements(80, 65, 30.4)
weatherData.setMeasurements(82, 70, 29.2)
weatherData.setMeasurements(78, 90, 29.2)
Пример #29
0
 def setUp(self):
     self.wd = WeatherData()
Пример #30
0
    def doUpload(self, **params):
        count = cherrypy.session.get("count", 0) + 1
        cherrypy.session["count"] = count
        sess = cherrypy.session
        sId = sess._id

        # run form submission validation
        errs = self.validate(params)
        if (len(errs) != 0):
            template = env.get_template("upload.html")
            return template.render({
                "errs": errs,
                "params": params,
                "formOptions": self.formOptions
            })

        # add the form data to the session so it is accessible until the end of the session
        sess.update(params)
        userDir = getUserDir()
        if not os.path.exists(userDir):
            os.makedirs(userDir)  # create the directories as needed
        upFile = params.get("upFile", None)
        ext = upFile.filename.split(".")[-1]
        rawFilePath = os.path.join(userDir, "raw_GB_upload.%s" % (ext))
        size = 0
        with open(rawFilePath, "wb") as rawFile:
            while True:
                data = upFile.file.read(8192)
                if not data: break
                size += len(data)
                rawFile.write(data)
        print "uploaded file to: ", rawFilePath
        parseTargetFile = getDataFile(ext=ext)
        try:
            if (
                    zipfile.is_zipfile(rawFilePath)
            ):  # try to find the electric interval data inside with a couple of heuristics
                # note that PGE's format looks like this "pge_electric_interval_data_2013-02-01_to_2013-04-28.xml"
                # note that SDGE's format looks like this "SDGE_Electric_15_Minute_06-30-2012_07-30-2013_20130801103424053.xml"
                matchPatterns = (".*electric.*\.xml", ".*electric.*\.csv"
                                 )  # only PGE & SDGE for now. Could add more.
                zf = zipfile.ZipFile(rawFilePath, "r")
                matched = None
                for innerFile in zf.namelist():
                    print(innerFile)
                    for pattern in matchPatterns:
                        match = re.match(
                            pattern, innerFile, re.I
                        )  # note "re.match" searches from the beginning of the string, "re.search" scans. re.I makes the match case insensitive.
                        if match is not None:
                            matched = match
                            ext = match.string.split(".")[-1]
                            parseTargetFile = getDataFile(ext=ext)
                            with open(parseTargetFile, "wb") as target:
                                source = zf.open(innerFile, "r")
                                try:
                                    shutil.copyfileobj(source, target)
                                finally:
                                    source.close()
                            break
                if matched is None:
                    raise Exception(
                        """Unrecognized zip format. There are many providers of Smart Meter data,
                                   and they use whatever file naming convention they want so we don't always
                                   know what file to look for inside the zip file. Please report this via
                                   the feedback page, and try again with your xml file unzipped."""
                    )
            else:  # if(ext == "xml"):
                try:
                    os.remove(
                        parseTargetFile
                    )  # in case a file was already uploaded during this session, get it out of the way
                    # rename won"t overwrite an existing file on Windows... see os.rename
                except:
                    pass  # if it doesn"t exist, no problem. If it is currently in use, then next line will fail.
                os.rename(rawFilePath, parseTargetFile)
            #else: raise Exception("Unrecognized file type %s" % ext)

            parsedData = analysis.parseDataFile(parseTargetFile)

            dates = parsedData.getReadings()[0]
            dateDiff = [j - i for i, j in zip(dates[:-1], dates[1:])]
            #print dateDiff
            if (min(dateDiff) > datetime.timedelta(hours=1)):
                raise Exception(
                    "Time difference must be at most 1 hour between readings")

        #except xml.parsers.expat.ExpatError as ee:
        #  print ee
        #  errs["upFile"] = "Your file does not appear to be in a Green Button XML format. Please check the file and try again."
        except Exception as e:
            print e
            errs["upFile"] = str(e)

        if (len(errs) != 0):
            template = env.get_template("upload.html")
            return template.render({
                "errs": errs,
                "params": params,
                "formOptions": self.formOptions
            })

        params["filename"] = upFile.filename
        params["filesize"] = size
        params["filetype"] = str(upFile.content_type)
        params["upload_time"] = datetime.datetime.now()
        del params["upFile"]  # remove the upload file obj
        # write params to working dir so session can be recreased
        with open(os.path.join(userDir, 'params.pkl'), 'wb') as paramFile:
            pickle.dump(params, paramFile)
        #with open(os.path.join(userDir,'params.pkl'),'rb') as paramFile:
        #  print pickle.load(paramFile)
        bldg = Building(parsedData.getReadings(), params['bldg_zip'], params)
        cherrypy.session["building"] = bldg
        weather = WeatherData("weather")
        first = bldg.days[0]
        last = bldg.days[-1]
        # warning. This can trigger a long download that is not in a separate thread
        bestWBAN = weather.closestWBAN(int(params["bldg_zip"]), first.year,
                                       first.month)
        sess["bestWBAN"] = bestWBAN
        bldg.attr["bestWBAN"] = bestWBAN

        pm = PlotMaker(bldg, getUserDir(),
                       cherrypy.config.get("wkhtmltopdf.bin"), sId)
        threading.Thread(target=pm.generateFiles).start()
        time.sleep(0.25)  # let the lock file get written
        sess["filename"] = upFile.filename
        sess["filesize"] = size
        sess["filetype"] = upFile.content_type
        sess["filepath"] = parseTargetFile
        template = env.get_template("dataExplore.html")
        response_dict = {}
        response_dict.update(sess)
        return self.explore()
Пример #31
0
        self.move_servo(servo_position)
        self.set_led_status(led_status)

    def move_servo(self, servo_position=0):
        self.servo.value = self.convert_percentage_to_integer(servo_position)

    def turnOffServo(self):
        sleep(2)
        self.servo.close()

    def set_led_status(self, led_status=0):

        if (led_status == 0):
            self.led.off()
        elif (led_status == 1):
            self.led.on()
        else:
            self.led.blink()

    def convert_percentage_to_integer(self, percentage_amount):
        #adjust for servos that turn counter clockwise by default
        adjusted_percentage_amount = 100 - percentage_amount
        return (adjusted_percentage_amount * 0.02) - 1


if __name__ == "__main__":

    weather_data = WeatherData('Yekaterinburg')
    weather_dashboard = WeatherDashboard(weather_data.getServoValue(),
                                         weather_data.getLEDValue())
    weather_dashboard.turnOffServo()
Пример #32
0
class TestWeatherData(unittest.TestCase):

    def setUp(self):
        self.wd = WeatherData()

    def testGetWindVelocity(self):
        # setup
        # define start/stop times
        start = (2007,11,30,0,0,0)
        end = (2007,12,30,0,0,0)
        expMJD = [ 54434.00000041
                ,  54434.00001201
                ,  54434.00002358
                ,  54434.00003515]
        expWind = [ 2.57343006,  2.87351751,  2.85987568,  2.87351751]

        # test
        time, wind = self.wd.getWindVelocity((start,end))
    
        for i in range(4):
            self.assertAlmostEquals(expMJD[i], time[i], 2)
            self.assertAlmostEquals(expWind[i], wind[i], 2)

    def testGetLastHourMedianWindSpeeds(self):

        # for when?
        now = datetime(2007,11,30,0,0,0)

        # test a specific time
        wind = self.wd.getLastHourMedianWindSpeeds(now)

        self.assertAlmostEquals(2.38246560, wind, 4)

        # make sure it changes by looking at the *current* wind
        wind = self.wd.getLastHourMedianWindSpeeds()

        self.assertNotEqual(2.38246560, wind)

    def testHourDanaMedianSpeeds(self):

        dt = datetime(2010, 6, 7, 12) # UTC
        m = self.wd.getHourDanaMedianSpeeds(dt)
        self.assertAlmostEquals(3.74649739265, m, 4)

        dt = datetime(2009, 6, 7, 12) # UTC
        m = self.wd.getHourDanaMedianSpeeds(dt)
        self.assertAlmostEquals(0.52738451957702637, m, 4)

    def testDanaMedian(self):

        # simple tests first
        data = [1.0 for i in range(3600)]
        m = self.wd.danaMedian(data)
        self.assertEqual(1.0, m)

        data = [1.0 for i in range(110)]
        m = self.wd.danaMedian(data)
        self.assertEqual(1.0, m)

        data = [float(i) for i in range(3600)]
        m = self.wd.danaMedian(data)
        self.assertEqual(3249.5, m)
Пример #33
0
print("Config loaded")

max_rows_per_insert = 10000
repo = DataRepository.DataRepository(config.db_info, max_rows_per_insert)
print(repo.test_connection())

# Process wait time data if flag set
if ("--wait" in sys.argv):
    print("Not implemented - Wait time data.")
    print("Probably not going to refactor this part.")

# Process air quality data if flag set
if ("--air" in sys.argv):
    AirData.send_data_to_db(repo)

# Process Economic data if flag set
if ("--econ" in sys.argv):
    print("Not implemented - Economic data")

# Process Oil/Gas data if flag set
if ("--petrol" in sys.argv):
    print("Not implemented - Oil/gas data")

# Process weather data
if ("--weather" in sys.argv):
    WeatherData.send_data_to_db(repo)

if ("--new_weather" in sys.argv):
    NewWeatherData.send_data_to_db(repo)

input("Completed succesfully")
Пример #34
0
import sys
from AlertData import AlertData
from WeatherData import WeatherData
from WaterData import WaterData
from StatisticData import StatisticData
from ElevationData import ElevationData
from pymongo import MongoClient
import json
import requests

if __name__ == "__main__":
    conn = MongoClient()
    db = conn["RiverLog"]
    alert = AlertData(db)
    water = WaterData(db)
    weather = WeatherData(db)
    statistic = StatisticData(db)
    elevation = ElevationData(db)
        
    #weather.ProcessRain("data/rain.xml")
    #water.Init()
    #water.ProcessWaterLevel("data/waterLevel.json")
    #water.ProcessReservoir("data/reservoir_2018-09-01_12.json")
    
    args = sys.argv
    if "init" in args:
        weather.Init()
        water.Init()
        alert.Init()
        
    if "collect10min" in args:
Пример #35
0
 def getWeather(self):
     weather = WeatherData()
     return weather
Пример #36
0
class Weather2DBImport:
    """
    This class contains logic to populate the weather database with
    GBT weather data.
    """

    def __init__(self, dbname = ""):
        self.c           = pg.connect(user = "******"
                                    , dbname = dbname
                                    , port   = settings.DATABASE_PORT
                                    )
        self.weatherData = WeatherData()
        self.pyrgeometerData = PyrgeometerData()

    def getNeededWeatherDates(self, dt = None):
        """
        Get's those dates that don't have any accompanying weather data.
        """
        if dt is None:
            dt = datetime.utcnow()        
        r = \
            self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM gbt_weather)
                               AND date <= '%s'
                         """ % dt)
        return [(row['id'], row['date']) for row in r.dictresult()]

    def getNeededWeatherDatesInRange(self, start, end):
        """
        Get's those dates that don't have any accompanying weather data.
        """
        r = self.c.query("""
                         SELECT id, date
                         FROM weather_dates
                         WHERE id NOT IN (SELECT weather_date_id
                                          FROM gbt_weather)
                               AND date >= '%s'
                               AND date <  '%s'
                         """ % (start, end))
        return [(row['id'], row['date']) for row in r.dictresult()]

    def insert(self, weatherDateId, wind, irradiance):
        """
        Inserts a row of data into the weather table.
        """
        # handle missing data - put in what you can
        windOK = wind and wind == wind
        irradianceOK = irradiance and irradiance == irradiance
        if windOK and irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,wind_speed,irradiance)
                    VALUES (%s, %s, %s)
                    """ % (weatherDateId, wind, irradiance)
        elif windOK and not irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,wind_speed)
                    VALUES (%s, %s)
                    """ % (weatherDateId, wind)
        elif not windOK and irradianceOK:
            query = """
                    INSERT INTO gbt_weather (weather_date_id,irradiance)
                    VALUES (%s, %s)
                    """ % (weatherDateId, irradiance)
        if windOK or irradianceOK:
            self.c.query(query)

    def update(self):
        """
        Looks to see what weather times need updating, then retrieves that
        data from the sampler logs, and finally writes results to DB.
        """
        
        results = []

        # look for any missing data within the last year
        end   = datetime.utcnow()
        start = end - timedelta(days = 365)
        dts = self.getNeededWeatherDatesInRange(start, end)

        for dtId, dtStr in dts:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            try:
                wind = self.weatherData.getHourDanaMedianSpeeds(dt)
            except:
                continue
            di   = self.pyrgeometerData.getHourMedianDownwardIrradiance(dt)
            results.append((dtId, wind, di))
            self.insert(dtId, wind, di)
        return results    

    def findNullValues(self, column):
        "Who is missing a value?"
        query = """
                SELECT gbt.id, wd.date 
                FROM gbt_weather AS gbt, weather_dates AS wd
                WHERE gbt.%s is NULL AND gbt.weather_date_id = wd.id
                """ % column
        r = self.c.query(query)
        return [(row['id'], row['date']) for row in r.dictresult()]
              
    def updateRow(self, rowId, column, value):
        """
        Updates a row in the weather table with a value.
        """
        query = """
                UPDATE gbt_weather SET %s = %s WHERE id = %d
                """ % (column, value, rowId)
        self.c.query(query)

    def backfill(self, column, callback, test = False):
        """
        Generic method for looking for null values in the weather table,
        and updating those rows with the appropriate value from the 
        sampler logs.
        """

        results = []
        missing = self.findNullValues(column)
        for id, dtStr in missing:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            v = callback(dt)
            # watch for NaN values
            if v and v == v:
                results.append((id, dtStr, v))
                if not test:
                    self.updateRow(id, column, v)
        return results 
   
    def backfillWind(self, test = False):
        return self.backfill("wind_speed"
                           , self.weatherData.getLastHourMedianWindSpeeds
                           , test)

    
    def backfillIrradiance(self):
        return self.backfill("irradiance"
                   , self.pyrgeometerData.getLastHourMedianDownwardIrradiance)
    

    def backfillReport(self, filename):
        "Backfills the DB, and creates report on results."

        # NOTE: current results for this method: using the weather or
        # or weather_unit_test DB's, since we only have 2006 & 2009 - present
        # data in those, and there is no 2006 pygeometer data, this method
        # only backfills in 2009 - present

        f = open(filename, 'w')
        # wind
        lines = []
        lines.append("Wind Speed\n")
        lines.append("Start (ET): %s\n" % datetime.now())
        results = self.backfillWind()
        for r in results:
            lines.append("%s,%s,%s\n" % (r[0], r[1], r[2]))
        lines.append("End (ET): %s\n" % datetime.now())
        f.writelines(lines)    
        # irradiance
        lines = []
        lines.append("Irradiance\n")
        lines.append("Start (ET): %s\n" % datetime.now())
        results = self.backfillIrradiance()
        for r in results:
            lines.append("%s,%s,%s\n" % (r[0], r[1], r[2]))
        lines.append("End (ET): %s\n" % datetime.now())
        f.writelines(lines)    
        f.close()    
        print "printed report to: ", filename

    def backfillDatabase(self, starttime, endtime):
        """
        Acquires the needed data whose dates that don't have any
        accompanying weather data, and inserts it into the database.
        """
        dts = self.getNeededWeatherDatesInRange(starttime, endtime)
        for dtId, dtStr in dts:
            dt = datetime.strptime(dtStr, "%Y-%m-%d %H:%M:%S")
            # Is there wind data?
            try:
                wind = self.weatherData.getHourDanaMedianSpeeds(dt)
            except:
                continue
            di = self.pyrgeometerData.getHourMedianDownwardIrradiance(dt)
            # Is irradiance a NaN?
            if di != di:
                di = None
            print dt, wind, di
            self.insert(dtId, wind, di)
Пример #37
0
 def __init__(self, dbname = "weather"):
     self.c           = pg.connect(user = "******"
                                 , dbname = dbname)
     self.weatherData = WeatherData()
                                ' align='middle'/></h3></div>
                             <div class="card-body">
                                <div class="row">
                                    <div class="col element-box">
                                        <h5>Temperature</h5>
                                        <p>""" + self.currentWeather.getTemperature() + """</p>
                                    </div>
                                    <div class="col element-box">
                                        <h5>Conditions</h5>
                                        <p>""" + self.currentWeather.getWeatherConditions() + """</p>
                                    </div>
                                    <div class="col element-box">
                                        <h5>Wind Speed</h5>
                                        <p>""" + self.currentWeather.getWindSpeed() + """</p>
                                    </div>
                                </div>
                            </div>
                            <div class="card-footer">""" + self.currentWeather.getTime() + """</div>
                        </div>
                    </div>
                </body>

                </html>
               """
    
if __name__=="__main__":
    currentWeather = WeatherData('Los Angeles')
    cherrypy.quickstart(WeatherDashboardHTML(currentWeather))
    

    
Пример #39
0
    def __init__(self):
        Gtk.Window.__init__(self, title="Magic Mirror")

        self.state_list = [
            "HOME", "WEATHER", "TIME", "QUOTE", "CALENDAR", "HELP", "INFO",
            "MIRROR"
        ]
        self.state = ""

        # Data classes which update from subscriber
        input_data = InputData()
        weather_data = WeatherData()
        quote_data = QuoteData()
        calendar_data = EventData()
        message_data = MessageData()
        home_data = HomeData()
        self.auth_data = AuthData()
        pin_data = PinData()

        # Screen objects that are cycled in the window
        self.home_screen = Home(weather_data, calendar_data, home_data,
                                self.auth_data)
        self.auth_screen = Auth(self.auth_data, pin_data)
        self.weather_screen = Weather(weather_data)
        self.time_screen = TimeDate()
        self.message_screen = Messages(message_data)
        self.quote_screen = Quote(quote_data)
        self.calendar_screen = Calendar(calendar_data)
        self.help_screen = Help()
        self.info_screen = Info()
        self.mirror_screen = Mirror()

        # Starts the MQTT subscriber
        self.data_thread = threading.Thread(target=subscriber.run,
                                            args=([
                                                input_data, weather_data,
                                                quote_data, calendar_data,
                                                home_data, self.auth_data,
                                                pin_data
                                            ], ))
        self.data_thread.daemon = True
        self.data_thread.start()

        # Updates the value on the screens in separate threads
        GObject.timeout_add(1000, self.auth_screen.update)
        GObject.timeout_add(1000, self.weather_screen.update_weather)
        GObject.timeout_add(1000, self.time_screen.update_clock)
        GObject.timeout_add(1000, self.quote_screen.update)
        GObject.timeout_add(1000, self.calendar_screen.update_events)
        GObject.timeout_add(1000, self.message_screen.update_screen)
        GObject.timeout_add(1000, self.home_screen.update_home)

        self.app_stack = Gtk.Stack()
        self.app_stack.set_transition_type(
            Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        self.app_stack.set_transition_duration(500)

        self.app_stack.add_named(self.auth_screen, "Auth")
        self.app_stack.add_named(self.home_screen, "Home")
        self.app_stack.add_named(self.weather_screen, "Weather")
        self.app_stack.add_named(self.time_screen, "Time")
        self.app_stack.add_named(self.message_screen, "Message")
        self.app_stack.add_named(self.quote_screen, "Quote")
        self.app_stack.add_named(self.calendar_screen, "Calendar")
        self.app_stack.add_named(self.help_screen, "Help")
        self.app_stack.add_named(self.info_screen, "Info")
        self.app_stack.add_named(self.mirror_screen, "Mirror")

        # Meant to add the default screen
        self.add(self.app_stack)

        self.fullscreen()
        self.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(255, 0, 255))
        self.set_icon(IMG.iconpix)
from WeatherData import WeatherData
from CurrentConditionsDisplay import CurrentConditionsDisplay

if __name__ == '__main__':
    weatherStation = WeatherData()
    currentConditionsDisplay = CurrentConditionsDisplay(weatherStation)

    weatherStation.setMeasurements(80, 65, 30.4)
    weatherStation.setMeasurements(82, 70, 29.2)
    weatherStation.setMeasurements(78, 90, 29.2)


Пример #41
0
 def __init__(self, year):
     self.dtFormat = "%Y-%m-%d %H:%M:%S"
     self.weatherData = WeatherData()
     self.start = datetime(year, 1, 1, 0, 0, 0)
     self.end = self.start + timedelta(hours=24 * 365)
Пример #42
0
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle("Smart mirror")
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.black)
        self.degree = unichr(176)
        self.redrawCount = 0
        self.calendarRefreshCount = 0
        self.setPalette(p)

        self.prevTrainLabel = QLabel(self)
        self.prevTrainLabel1 = QLabel(self)
        self.prevTrainLabel2 = QLabel(self)
        self.prevTrainLabel3 = QLabel(self)

        self.nextTrainLabel = QLabel(self)
        self.nextTrainLabel1 = QLabel(self)
        self.nextTrainLabel2 = QLabel(self)
        self.nextTrainLabel3 = QLabel(self)

        self.nextTrain1Label = QLabel(self)
        self.nextTrain1Label1 = QLabel(self)
        self.nextTrain1Label2 = QLabel(self)
        self.nextTrain1Label3 = QLabel(self)

        self.nextTrain2Label = QLabel(self)
        self.nextTrain2Label1 = QLabel(self)
        self.nextTrain2Label2 = QLabel(self)
        self.nextTrain2Label3 = QLabel(self)

        self.startTime = QLabel(self)
        self.lateTime = QLabel(self)
        self.arrivalTime = QLabel(self)
        self.arrivalStation = QLabel(self)
        self.line = QLabel(self)
        self.time = QLabel(self)
        self.date = QLabel(self)

        self.temperature = QLabel(self)
        self.weather = [QLabel(self), QLabel(self), QLabel(self)]
        self.clouds = QLabel(self)
        self.wind = QLabel(self)

        self.weatherForecastWeek = []
        self.forecastSeparatorLine = []
        self.forecastSeparatorPartOfTheDay = []
        self.forecastDayIdentify = []
        for i in range(4):
            self.forecastSeparatorLine.append(QLabel(self))
            self.forecastSeparatorPartOfTheDay.append(QLabel(self))
        for i in range(5):
            self.forecastDayIdentify.append(QLabel(self))
        for i in range(5 * forecastRows):
            self.weatherForecastWeek.append(QLabel(self))

        self.calendarEntries = []

        for i in range(15):
            self.calendarEntries.append(QLabel(self))

        self.trainLabelFont = QtGui.QFont("Times", 35, QtGui.QFont.Normal)
        self.dayLabelFont = QtGui.QFont("Times", 30, QtGui.QFont.Normal)
        self.timeLabelFont = QtGui.QFont("Times", 150, QtGui.QFont.Normal)
        self.tempLabelFont = QtGui.QFont("Times", 100, QtGui.QFont.Normal)
        self.dateLabelFont = QtGui.QFont("Times", 45, QtGui.QFont.Normal)
        self.forecastLabelFont = QtGui.QFont("Times", 20, QtGui.QFont.Normal)
        self.forecastLabelFontBold = QtGui.QFont("Times", 20, QtGui.QFont.Bold)

        exit_action = QAction("", self)
        exit_action.setShortcut("Ctrl+Q")
        exit_action.setStatusTip('')
        exit_action.triggered.connect(self.showNormalAndExit)
        exit_full_screen = QAction("", self)
        exit_full_screen.setShortcut("Ctrl+X")
        exit_full_screen.setStatusTip('')
        exit_full_screen.triggered.connect(self.showNormal)
        return_full_screen = QAction("", self)
        return_full_screen.setShortcut("Ctrl+A")
        return_full_screen.setStatusTip('')
        return_full_screen.triggered.connect(self.showFullScreen)

        self.colorWhite = 'color: white'
        self.colorGrey = 'color: grey'
        self.colorRed = 'color: red'
        self.colorDarkGrey = 'color: #1e1e1e'

        self.statusBar()
        self.setStyleSheet("""
            QMenuBar {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
                border: 1px solid #000;
            }

            QMenuBar::item {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
            }

            QMenuBar::item::selected {
                background-color: rgb(0,0,0);
            }

            QMenu {
                background-color: rgb(0,0,0);
                color: rgb(255,255,255);
                border: 1px solid #000;           
            }

            QMenu::item::selected {
                background-color: rgb(0,0,0);
            }
        """)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('')
        fileMenu.addAction(exit_action)
        fileMenu.addAction(exit_full_screen)
        fileMenu.addAction(return_full_screen)
        mainMenu.resize(0, 0)

        self.showFullScreen()
        self.trainData = TrainData()
        self.weatherData = WeatherData()
        self.setTrainTexts()
        self.initTrainsLabel()
        self.initDateTimeLabels()
        self.initWeatherLabels()
        self.initForecastWeatherLabels()
        self.initCalendarLabels()
        self.setTrains()
        self.setDateTime()
        self.setWeather()
        self.setForecastWeather()
        self.setCalendar()
"""
Autor: GKW Agro Tech
Titulares:
    GKW Agro Tech
    MarcoA Instalacao de Sistemas de Aquecimento Ltda
Create Date: 11/11/2019
Update Date: 23/12/2019

Aplicacao que tem por objetivo obter todas as linhas cadastradas
obter a media de consumo diaria e armazenar a mesma em uma nova
variavel no banco de dados
"""

from db_config import connection
from db_store import *
from WeatherData import WeatherData

ids = selectIdRow(connection)
for i in range(len(ids)):
    linha = int(ids[i][0])
    consum = selectAvgDalyConsum(connection, linha)

    if consum[0][0] is not None:
        c = consum[0][0]
        # A partir dos dados obtidos anteriormente, armazena as informacoes na respectiva tabela
        weatherData = WeatherData(linha, int(10), c)
        insertWeatherData(connection, weatherData)

        # print("Linha: " + str(linha) + " CONSUMO: " + str(c))