如何解释重复的小时记录wundergroundapi

2024-06-13 08:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

对于特定的历史查询:http://api.wunderground.com/api/MY_API_KEY/history_20131231/geolookup/q/Beijing/Beijing.json

我得到一个小时内周期性的重复数据。例如,2014年1月1日下午2:00,将有两行具有不同数据,例如:

"Year", "Month", "Day", "Hour", "Min", "Humidity", "Temperature_F", "Windspeed_mph", "Wind_direction_deg", "Air_Pressure_mb"
"2014","01","01","14","00","7","53","6.7","250","1014"
"2014","01","01","14","00","13","51.8","17.9","300","1014"

我认为这不是我的代码的问题,而是api的问题。我该怎么解释?不管怎样,这是我的代码:

'''
Takes a date and file name, returns an hourly print out of historical weather
data, including humidity, temperature, wind speed, wind direction, and air      pressure
'''

def print_column_headings(headers, file_name):
    with open (file_name, 'wb') as initial_file:
        w = csv.writer(initial_file, quoting=csv.QUOTE_ALL)
        w.writerow(headers)
    initial_file.close()

def print_one_day_of_weather_data(year, month, day, max_rows, file_to_write):

# get data
url = 'http://api.wunderground.com/api/<MY_API_KEY>/history_' + year + month + day +'/geolookup/q/Beijing/Beijing.json'
f = urllib2.urlopen(url)
print url
json_string = f.read()
parsed_json = json.loads(json_string)

# paramterize data
all_obs = parsed_json['history']['observations']

# if we reach the end of the observations
for n in range(0, len(all_obs)):
    # or we exceed the max rows desired
    if n > max_rows:
        return 0
    else: 
        base_path = all_obs[n]
        date_path = base_path['date']
        year = date_path['year']
        month = date_path['mon']
        day = date_path['mday']
        hour = date_path['hour']
        min = date_path['min']
        humidity = base_path['hum']
        temp_f = base_path['tempi']
        windspeed_mph = base_path['wspdi']
        winddir_deg = base_path['wdird']
        air_pressure_mb = base_path['pressurem']

        # utc time
        params = [year, month, day, hour, min, humidity, temp_f, windspeed_mph, winddir_deg, air_pressure_mb]

        print params

        with open (file_to_write, 'a') as csvfile:
            w = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
            w.writerow(params)

        csvfile.close()


headers = ("Year", "Month", "Day", "Hour", "Min", "Humidity", "Temperature_F", "Windspeed_mph", "Wind_direction_deg", "Air_Pressure_mb")

def append_leading_zeroes(num):
return "%02d" % (num,)

def days_in_a_month(year, month_num):
    return calendar.monthrange(year, month_num)[1]

def file_namer(city, month_num, year): 
    return "raw weather - local time " + city + "-" + calendar.month_name[month_num] + "-" + str(year) + ".csv"

def gen_entire_month(city, month, year, should_print_headers, start_at_day=1):
    file_name = file_namer(city, month, year)

    if should_print_headers:
      print_column_headings(headers, file_name)

  for day in range(start_at_day, days_in_a_month(year, month) + 1):
    print_one_day_of_weather_data(str(year), append_leading_zeroes(month), append_leading_zeroes(day), 100, file_name)      
    # if we make too many calls in a short period of time, the API refuses are calls
    time.sleep(60)

gen_entire_month("Beijing", 1, 2014, should_print_headers=True)

Tags: pathnameapijsonbasedatedefyear
1条回答
网友
1楼 · 发布于 2024-06-13 08:29:09

我们从2个新气象站或世界气象组织获得了一些obs。查看“metar”字段,只使用以“metar…”开头的读数,因为这些读数来自obs站。那些从其他任何东西开始的,比如AAXX,都是特殊读物。它们也是合法的,但是通过混合这两种来源,它会让人困惑。你知道吗

相关问题 更多 >