コード例 #1
0
ファイル: forecast.py プロジェクト: crvv/weather
    def __init__(self, locale, site):
        site_id = str(site, 'utf-8')  # 地点的id
        site_x = site_id[3:7]
        site_y = site_id[0:3]
        self.zone = timezone(site_x)  # 计算时区
        data_base = SQLiteDataBase(site_y)  # 新建一个数据库对象
        self.first_date_hour = get_first_date_hour(self.zone)  # 第一个时间点
        data = data_base.read(site_x, self.first_date_hour)  # 从数据库中获取数据
        # data的格式是[(2014120400, 2, 26695, 346, -119, 0, 624000, 103220), (...), ...]
        data_base.close()
        cloud_data = []
        temperature_data = []
        precipitation_data = []
        wind_speed_data = []
        wind_direction_data = []

        for i in range(24):
            cloud_data.append(data[i][1])  # int %
            temperature_data.append(round(data[i][2] / 100 - 273.15))  # int Celsius degree
            precipitation_data.append(round(data[i][5] / 1e6 * 86400))  # int mm/24h
            wind_speed_data.append(math.hypot(data[i][3] / 100, data[i][4] / 100))  # float m/s

            direction = math.atan2(data[i][3], data[i][4]) / math.pi * 180 + 360
            wind_direction_data.append(round(direction % 360))  # int degree

        self.temperature = Temperature(temperature_data)
        self.cloud = Cloud(cloud_data)
        self.precipitation = Precipitation(precipitation_data)
        self.wind = Wind(wind_speed_data, wind_direction_data)
コード例 #2
0
ファイル: forecast.py プロジェクト: crvv/weather
class Forecast():
    def __init__(self, locale, site):
        site_id = str(site, 'utf-8')  # 地点的id
        site_x = site_id[3:7]
        site_y = site_id[0:3]
        self.zone = timezone(site_x)  # 计算时区
        data_base = SQLiteDataBase(site_y)  # 新建一个数据库对象
        self.first_date_hour = get_first_date_hour(self.zone)  # 第一个时间点
        data = data_base.read(site_x, self.first_date_hour)  # 从数据库中获取数据
        # data的格式是[(2014120400, 2, 26695, 346, -119, 0, 624000, 103220), (...), ...]
        data_base.close()
        cloud_data = []
        temperature_data = []
        precipitation_data = []
        wind_speed_data = []
        wind_direction_data = []

        for i in range(24):
            cloud_data.append(data[i][1])  # int %
            temperature_data.append(round(data[i][2] / 100 - 273.15))  # int Celsius degree
            precipitation_data.append(round(data[i][5] / 1e6 * 86400))  # int mm/24h
            wind_speed_data.append(math.hypot(data[i][3] / 100, data[i][4] / 100))  # float m/s

            direction = math.atan2(data[i][3], data[i][4]) / math.pi * 180 + 360
            wind_direction_data.append(round(direction % 360))  # int degree

        self.temperature = Temperature(temperature_data)
        self.cloud = Cloud(cloud_data)
        self.precipitation = Precipitation(precipitation_data)
        self.wind = Wind(wind_speed_data, wind_direction_data)

    def get_forecast(self):
        """


        :return: 第一个返回值是字符串长度列表,列表的第一个元素是总长度
        """
        forecast = bytes()
        l = []
        string_list = self.get_date_list() + self.cloud.forecast() + \
                      self.temperature.forecast() + self.precipitation.forecast() + \
                      self.wind.forecast()
        for string in string_list:
            f = string.encode('utf-8')
            forecast = forecast + f
            l.append(len(f))
        length_list = [len(forecast)]
        length_list.extend(l)
        return bytes(length_list), forecast

    def get_date_list(self):
        date_format = '%Y-%m-%d  %H'

        today = datetime.strptime(self.first_date_hour, '%Y%m%d%H') + timedelta(hours=self.zone)
        tomorrow = today + timedelta(days=1)
        day_after_tomorrow = today + timedelta(days=2)

        today = today.strftime(date_format)
        tomorrow = tomorrow.strftime(date_format)
        day_after_tomorrow = day_after_tomorrow.strftime(date_format)
        return [today, tomorrow, day_after_tomorrow]