Beispiel #1
0
def cleancloudCover(province, city):
    cloudCover = "cloudCover"
    sql = "SELECT wdate,hightemper,lowtemper,iconstate,humidity,cloudCover,windSpeed \
            FROM weatherdata WHERE province = '%s' AND city = '%s' AND \
            cloudCover<>999.98999 ORDER BY wdate ASC" % (province, city)
    table = gettable(sql)
    data_x, data_y = get_data(table)
    ws = standRegress(data_x, data_y)
    print("ws(相关系数):", ws)  # ws 存放的就是回归系数
    insertsql = "INSERT INTO cleandata(province,city,factor,hightemper,\
    lowtemper,iconstate,humidity,windspeed) VALUES ('%s', '%s','%s',%f,%f,%f,%f,%f );"                                                                                       % \
                (province, city,cloudCover,ws[0],ws[1],ws[2],ws[3],ws[4])
    insertall(insertsql)
Beispiel #2
0
def updatecloudCover(province, city):
    cloudCover = "cloudCover"
    sql = "SELECT wdate,hightemper,lowtemper,iconstate,humidity,cloudCover,windSpeed \
                FROM weatherdata WHERE province = '%s' AND city = '%s' AND cloudCover=999.98999 \
                ORDER BY wdate ASC" % (province, city)
    xishusql = "SELECT province,city,factor,hightemper,lowtemper,iconstate,humidity,windspeed \
                FROM cleandata WHERE province = '%s' AND city = '%s' AND factor='%s'"                                                                                      %\
               (province, city,cloudCover)
    ws = selectarray(xishusql)
    ws = np.reshape(ws, [-1])
    ws = np.array(ws[3:8], dtype=np.float32)
    ws = np.mat(ws).T
    table = gettable(sql)
    datetable = table["wdate"]
    data_x, data_y = get_data(table)
    print(datetable)
    yHat = np.mat(data_x) * ws
    for i in range(len(yHat)):
        updatesql = "UPDATE weatherdata SET cloudCover = %f \
         WHERE province = '%s' AND city = '%s' AND wdate = '%s';" % (
            yHat[i], province, city, datetable[i])
        insertall(updatesql)
Beispiel #3
0
def getcity():
  #  sql=[]
    for i in PROVINCE_DICT:
        p=PROVINCE_DICT[i]
        t = CITY.get(i, ())
        if(len(t)==0):
            c=PROVINCE_DICT[i]
            piny=change(c)
            po=getposition(c)
            lon=po[0]
            lat=po[1]
            strsql = "INSERT INTO citydata(province,city,pinyin,jingdu,weidu) VALUES ('%s', '%s','%s',%f,%f )" % (p, c,piny,lon,lat)
            strsql+=";"
            insertall(strsql)
        else:
            for j in range(len(t)):
                c=t[j][1]
                piny = change(c)
                po = getposition(c)
                lon = po[0]
                lat = po[1]
                strsql = "INSERT INTO citydata(province,city,pinyin,jingdu,weidu) VALUES ('%s', '%s','%s',%f,%f )" % (p, c, piny, lon, lat)
                strsql+=";"
                insertall(strsql)
Beispiel #4
0
def updateweather(province, city):
    apikey = getapikey()
    sql = "SELECT MAX(wdate) FROM weatherdata \
    WHERE province = '%s' AND city = '%s' ORDER BY wdate DESC;" % (province,
                                                                   city)
    global format
    format = "%Y-%m-%d"
    lasttime = selectone(sql)
    lasttime = datetime.datetime.strptime(str(lasttime), format)
    nowtime = getnowtime()
    nowtime = datetime.datetime.strptime(nowtime, format)
    delta = (nowtime - lasttime).days
    if delta > 0:
        ssql = "SELECT jingdu,weidu from citydata WHERE province='%s' AND city='%s'" % (
            province, city)
        lon, lat = selectdata(ssql)
        for i in range(1, delta):
            strtime = gettimestep(i)
            fio = ForecastIO.ForecastIO(
                apikey,
                extend=(ForecastIO.ForecastIO.EXCLUDE_DAILY, ),
                exclude=(ForecastIO.ForecastIO.EXCLUDE_CURRENTLY,
                         ForecastIO.ForecastIO.EXCLUDE_HOURLY,
                         ForecastIO.ForecastIO.EXCLUDE_MINUTELY,
                         ForecastIO.ForecastIO.EXCLUDE_FLAGS),
                units=ForecastIO.ForecastIO.UNITS_SI,
                lang=ForecastIO.ForecastIO.LANG_CHINESE,
                latitude=lat,
                longitude=lon,
                time_url=strtime)
            print(fio.get_url())
            if fio.has_daily() is True:
                daily = FIODaily.FIODaily(fio)
                try:
                    # 最高温
                    temperatureHigh = daily.get_day(0)["temperatureHigh"]
                except:
                    temperatureHigh = 999.99

                try:
                    # 最低温
                    temperatureLow = daily.get_day(0)["temperatureLow"]
                except:
                    temperatureLow = 999.99

                try:
                    # 湿度
                    humidity = daily.get_day(0)["humidity"]
                except:
                    humidity = 999.99

                try:
                    # 总体情况
                    icon = daily.get_day(0)["icon"]
                    # 描述转数据
                    ico = changeicon(icon)
                except:
                    ico = 0

                try:
                    # 最大降雨强度
                    precipIntensityMax = daily.get_day(0)["precipIntensityMax"]
                except:
                    precipIntensityMax = 999.99

                try:
                    # 日期
                    weatherdate = daily.get_day(0)["time"]
                    # 转换时间
                    wtime = changetime(weatherdate)
                except:
                    wtime = "9999-99-99"

                try:
                    # 降雨概率
                    precipProbability = daily.get_day(0)["precipProbability"]
                except:
                    precipProbability = 999.99

                try:
                    # 云图
                    cloudCover = daily.get_day(0)["cloudCover"]
                except:
                    cloudCover = 999.99

                try:
                    # 风速
                    windSpeed = daily.get_day(0)["windSpeed"]
                except:
                    windSpeed = 999.99
                insertsql = "INSERT INTO weatherdata\
                        (wdate,city,province,hightemper,lowtemper,iconstate,humidity,\
                        precipProbability,precipIntensityMax,cloudCover,windSpeed) \
                        VALUES ('%s', '%s', '%s', %f, %f, %d, %f, %f, %f, %f, %f );"                                                                                     % \
                            (wtime, city, province, temperatureHigh, temperatureLow, ico,
                             humidity, precipProbability, precipIntensityMax, cloudCover, windSpeed)
                insertall(insertsql)
            else:
                print('No Daily data')
    else:
        print("数据已是最新,无需更新")
Beispiel #5
0
def getweather(province, city):
    apikey = getapikey()
    sql = "SELECT jingdu,weidu from citydata WHERE province='%s' AND city='%s'" % (
        province, city)
    lon, lat = selectdata(sql)
    for i in range(500):
        format = '%Y-%m-%d'
        value = time.time()
        dtime = value - 86400 * i
        dt = time.strftime(format, time.localtime(float(dtime)))
        strtime = dt + "T23:00:00"
        #print(strtime)
        fio = ForecastIO.ForecastIO(
            apikey,
            extend=(ForecastIO.ForecastIO.EXCLUDE_DAILY, ),
            exclude=(ForecastIO.ForecastIO.EXCLUDE_CURRENTLY,
                     ForecastIO.ForecastIO.EXCLUDE_HOURLY,
                     ForecastIO.ForecastIO.EXCLUDE_MINUTELY,
                     ForecastIO.ForecastIO.EXCLUDE_FLAGS),
            units=ForecastIO.ForecastIO.UNITS_SI,
            lang=ForecastIO.ForecastIO.LANG_CHINESE,
            latitude=lat,
            longitude=lon,
            time_url=strtime)
        #print('Latitude', fio.latitude, 'Longitude', fio.longitude)
        #print('Timezone', fio.timezone, 'Offset', fio.offset)
        print(fio.get_url())  # You might want to see the request url
        if fio.has_daily() is True:
            daily = FIODaily.FIODaily(fio)
            #print('Daily')
            #for item in daily.get_day(0).keys():
            #    print(item + ' : ' + str(daily.get_day(0)[item]))
            try:
                # 最高温
                temperatureHigh = daily.get_day(0)["temperatureHigh"]
            except:
                temperatureHigh = 999.99

            try:
                # 最低温
                temperatureLow = daily.get_day(0)["temperatureLow"]
            except:
                temperatureLow = 999.99

            try:
                #湿度
                humidity = daily.get_day(0)["humidity"]
            except:
                humidity = 999.99

            try:
                #总体情况
                icon = daily.get_day(0)["icon"]
                # 描述转数据
                ico = changeicon(icon)
            except:
                ico = 0

            try:
                #最大降雨强度
                precipIntensityMax = daily.get_day(0)["precipIntensityMax"]
            except:
                precipIntensityMax = 999.99

            try:
                #日期
                weatherdate = daily.get_day(0)["time"]
                # 转换时间
                wtime = changetime(weatherdate)
            except:
                wtime = "9999-99-99"

            try:
                # 降雨概率
                precipProbability = daily.get_day(0)["precipProbability"]
            except:
                precipProbability = 999.99

            try:
                #云图
                cloudCover = daily.get_day(0)["cloudCover"]
            except:
                cloudCover = 999.99

            try:
                #风速
                windSpeed = daily.get_day(0)["windSpeed"]
            except:
                windSpeed = 999.99
            insertsql="INSERT INTO weatherdata\
            (wdate,city,province,hightemper,lowtemper,iconstate,humidity,\
            precipProbability,precipIntensityMax,cloudCover,windSpeed) \
            VALUES ('%s', '%s', '%s', %f, %f, %d, %f, %f, %f, %f, %f );"                                                                         %\
                      (wtime, city, province, temperatureHigh, temperatureLow, ico,
                       humidity,precipProbability,precipIntensityMax,cloudCover,windSpeed)
            insertall(insertsql)
        else:
            print('No Daily data')