12

BigQuery 将 NOAA 的 gsod 数据加载为公共数据集 - 从 1929 年开始:https ://www.reddit.com/r/bigquery/comments/2ts9wo/noaa_gsod_weather_data_loaded_into_bigquery/

如何检索任何城市的历史数据?

4

4 回答 4

14

2019 年更新:为方便起见

SELECT * 
FROM `fh-bigquery.weather_gsod.all`
WHERE name='SAN FRANCISCO INTERNATIONAL A'
ORDER BY date DESC

每天更新 - 如果没有,请在此处报告

例如,要获得自 1980 年以来旧金山车站最热的日子:

SELECT name, state, ARRAY_AGG(STRUCT(date,temp) ORDER BY temp DESC LIMIT 5) top_hot, MAX(date) active_until
FROM `fh-bigquery.weather_gsod.all` 
WHERE name LIKE 'SAN FRANC%'
AND date > '1980-01-01'
GROUP BY 1,2
ORDER BY active_until DESC

在此处输入图像描述

请注意,由于有一个聚簇表,这个查询只处理了 28MB。

和类似的,但不使用站名,我将使用位置和按位置聚集的表:

WITH city AS (SELECT ST_GEOGPOINT(-122.465, 37.807))

SELECT name, state, ARRAY_AGG(STRUCT(date,temp) ORDER BY temp DESC LIMIT 5) top_hot, MAX(date) station_until
FROM `fh-bigquery.weather_gsod.all_geoclustered`  
WHERE EXTRACT(YEAR FROM date) > 1980
AND ST_DISTANCE(point_gis, (SELECT * FROM city)) < 40000
GROUP BY name, state
HAVING EXTRACT(YEAR FROM station_until)>2018
ORDER BY ST_DISTANCE(ANY_VALUE(point_gis), (SELECT * FROM city)) 
LIMIT 5

在此处输入图像描述


2017 年更新:标准 SQL 和最新表:

SELECT TIMESTAMP(CONCAT(year,'-',mo,'-',da)) day, AVG(min) min, AVG(max) max, AVG(IF(prcp=99.99,0,prcp)) prcp
FROM `bigquery-public-data.noaa_gsod.gsod2016`
WHERE stn='722540' AND wban='13904'
GROUP BY 1
ORDER BY day

附加示例,以显示十年来芝加哥最冷的日子:

#standardSQL
SELECT year, FORMAT('%s%s',mo,da) day ,min
FROM `fh-bigquery.weather_gsod.stations` a
JOIN `bigquery-public-data.noaa_gsod.gsod201*` b
ON a.usaf=b.stn AND a.wban=b.wban
WHERE name='CHICAGO/O HARE ARPT'
AND min!=9999.9
AND mo<'03'
ORDER BY 1,2

要检索任何城市的历史天气,首先我们需要找到该城市的气象站报告。该表[fh-bigquery:weather_gsod.stations]包含已知电台的名称、它们的州(如果在美国)、国家和其他详细信息。

因此,要查找德克萨斯州奥斯汀的所有电台,我们将使用如下查询:

SELECT state, name, lat, lon
FROM [fh-bigquery:weather_gsod.stations] 
WHERE country='US' AND state='TX' AND name CONTAINS 'AUST'
LIMIT 10

在此处输入图像描述

这种方法有两个问题需要解决:

  • 该表中并非每个已知电台都存在 - 我需要获取此文件的更新版本。因此,如果您在这里没有找到您要找的车站,请不要放弃。
  • 并非在此文件中找到的每个台站每年都在运行 - 因此我们需要找到在我们正在寻找的年份中有数据的台站。

为了解决第二个问题,我们需要将站表与我们正在寻找的实际数据连接起来。以下查询查找 Austin 周围的站点,该列c查看 2015 年有多少天有实际数据:

SELECT state, name, FIRST(a.wban) wban, FIRST(a.stn) stn, COUNT(*) c, INTEGER(SUM(IF(prcp=99.99,0,prcp))) rain, FIRST(lat) lat, FIRST(lon) long
FROM [fh-bigquery:weather_gsod.gsod2015] a
JOIN [fh-bigquery:weather_gsod.stations] b 
ON a.wban=b.wban
AND a.stn=b.usaf
WHERE country='US' AND state='TX' AND name CONTAINS 'AUST'
GROUP BY 1,2
LIMIT 10

在此处输入图像描述

那挺好的!我们在 2015 年发现了 4 个有奥斯汀数据的站点。

请注意,我们必须以一种特殊的方式处理“雨”:当一个站不监视下雨时,而不是null,它会将其标记为 99.99。我们的查询过滤掉这些值。

现在我们知道了这些站点的 stn 和 wban 编号,我们可以选择其中任何一个并可视化结果:

SELECT TIMESTAMP('2015'+mo+da) day, AVG(min) min, AVG(max) max, AVG(IF(prcp=99.99,0,prcp)) prcp
FROM [fh-bigquery:weather_gsod.gsod2015]
WHERE stn='722540' AND wban='13904'
GROUP BY 1
ORDER BY day

在此处输入图像描述

于 2016-01-15T04:57:57.950 回答
2

除了Felipe 的“官方”公共数据集之外,BigQuery现在还有一组官方的 NOAA 数据。有一篇博客文章描述了它

获取 2016 年 8 月 15 日最低温度的示例:

SELECT
  name, 
  value/10 AS min_temperature,
  latitude,
  longitude
FROM
  [bigquery-public-data:ghcn_d.ghcnd_stations] AS stn
JOIN
  [bigquery-public-data:ghcn_d.ghcnd_2016] AS wx
ON
  wx.id = stn.id
WHERE
  wx.element = 'TMIN'
  AND wx.qflag IS NULL
  AND STRING(wx.date) = '2016-08-15'

返回:

在此处输入图像描述

于 2016-09-22T16:02:59.777 回答
1

感谢您提取数据并将其设为公共表。这是一个 BigQuery,它返回 2014 年德克萨斯州每个站点的总降雨量:

SELECT FIRST(name) AS station_name, stn, SUM(prcp) AS annual_precip
FROM [fh-bigquery:weather_gsod.gsod2014] gsod
JOIN [fh-bigquery:weather_gsod.stations] stations
ON gsod.wban=stations.wban AND gsod.stn=stations.usaf
WHERE state='TX' AND prcp != 99.99
GROUP BY stn

返回: 结果表

拉入每个位置的下雨天数,并以此为基础对结果进行排序:

SELECT FIRST(name) AS station_name, stn, SUM(prcp) AS annual_precip,     COUNT(prcp) AS rainy_days
FROM [fh-bigquery:weather_gsod.gsod2014] gsod
JOIN [fh-bigquery:weather_gsod.stations] stations
ON gsod.wban=stations.wban AND gsod.stn=stations.usaf
WHERE state='TX' AND prcp != 99.99 AND prcp > 0
GROUP BY stn
ORDER BY rainy_days DESC

想出这些结果

于 2016-02-25T23:42:09.320 回答
0

使用站名是不可靠的。此外,使用新的 bigquery 功能很难使用地理空间查询,因为城市的边界没有清晰的形状(如圆形或矩形)。

因此,我为您的问题找到的最佳解决方案是使用反向地理编码,要求 Google Maps API 使用纬度/经度坐标为每个车站生成地址、州、市和县。

这是美国生成的 CSV ( StationNumber,Lat,Lon,Address,State,City,County,Zip)(您会注意到那里存在 98% 的电台): https ://gist.github.com/orcaman/a3e23c47489705dff93aace2e35f57d3

如果您想在美国以外的站点(golang)上重新运行它,下面是代码: https ://gist.github.com/orcaman/8de55f14f1c70ef5b0c124cf2fb7d9d1

于 2019-02-09T20:19:35.440 回答