Exemplo n.º 1
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)
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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))
Exemplo n.º 5
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))
Exemplo n.º 6
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)