weatheR数据包

时间:2017-07-13 10:50:13

标签: r api

使用weatheR包我需要来自特定站的数据,所以我尝试使用getStationByID功能。 该站是GROTTAGLIE,在我的station.list中,其美国空军的身份证是163240:

require(devtools)
install_github("mpiccirilli/weatheR")
require(weatheR)
station.list <- allStations()

station.list %>% filter(USAF==163240)


USAF     WBAN       NAME   CTRY   STATE    ICAO    LAT    LON      ELEV    BEGIN      END 
 163240   99999  GROTTAGLIE  IT           LIBG    40.518    17.403    65.5    19430927 20170710

如果我启动功能

grottaglie<- getStationByID(stationID = '163240',station.list = station.list, begin = 2013, 2016)

我得到了

  

名称错误(combined.list)[i]&lt; - keys [i]:       'names'属性[1]的长度必须与vector [0]

的长度相同

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您可以使用rnoaa

install.packages("rnoaa")
library(rnoaa)
(res <- isd(usaf=163240, wban=99999, year=1986))
#> # A tibble: 7,364 x 78
#>    total_chars usaf_station wban_station     date  time date_flag latitude longitude type_code elevation
#>          <chr>        <chr>        <chr>    <chr> <chr>     <chr>    <chr>     <chr>     <chr>     <chr>
#>  1        0058       163240        99999 19860101  0032         4   +40517   +017400     FM-15     +0069
#>  2        0058       163240        99999 19860101  0100         4   +40517   +017400     FM-15     +0069
#>  3        0058       163240        99999 19860101  0208         4   +40517   +017400     FM-15     +0069
#>  4        0058       163240        99999 19860101  0308         4   +40517   +017400     FM-15     +0069
#>  5        0058       163240        99999 19860101  0408         4   +40517   +017400     FM-15     +0069
#>  6        0074       163240        99999 19860101  0508         4   +40517   +017400     FM-15     +0069
#>  7        0074       163240        99999 19860101  0610         4   +40517   +017400     FM-15     +0069
#>  8        0074       163240        99999 19860101  0710         4   +40517   +017400     FM-15     +0069
#>  9        0074       163240        99999 19860101  0810         4   +40517   +017400     FM-15     +0069
#> 10        0074       163240        99999 19860101  0910         4   +40517   +017400     FM-15     +0069
#> # ... with 7,354 more rows, and 68 more variables: call_letter <chr>, quality <chr>,
#> #   wind_direction <chr>, wind_direction_quality <chr>, wind_code <chr>, wind_speed <chr>,
#> ...

答案 1 :(得分:1)

weatheR套餐似乎是一个废弃的项目 查看getStationByID函数的代码,可以意识到它无法工作 修改后的getStationByID可用here 下载此文件并将其保存在名为mygetStationByID.r的工作目录中 然后,使用以下代码:

require(weatheR)
station.list <- allStations()

# Load the modified version of getStationByID   
source("mygetStationByID.r")
# The structure of the fixed-width files 
# downloaded from ftp://ftp.ncdc.noaa.gov/pub/data/noaa/
col.width <- c(4,6,5,8,4,1,6,7,5,5,5,4)
# Column names
col.names <- c("TOT", "USAF", "WBAN", "DATE", "TIME", "DATASOURCE",
 "LAT","LON","CODE","ELEV.DIM","CALL.LETTER","QLT.CTRL")

grottaglie <- mygetStationByID(stationID = 163240,
             station.list = station.list, begin = 2013, end=2016)

str(grottaglie)
# List of 2
#  $ dl_status   :'data.frame':   4 obs. of  4 variables:
#   ..$ File  : chr [1:4] "163240-99999-2013.gz" "163240-99999-2014.gz" "163240-99999-2015.gz" "163240-99999-2016.gz"
#   ..$ Status: chr [1:4] "Failed" "Failed" "Failed" "Failed"
#   ..$ City  : chr [1:4] "Grottaglie, Italy" "Grottaglie, Italy" "Grottaglie, Italy" "Grottaglie, Italy"
#   ..$ rank  : num [1:4] 1 1 1 1
#  $ station_data:List of 1
#   ..$ Grottaglie_163240:'data.frame':   19264 obs. of  12 variables:
#   .. ..$ TOT        : int [1:19264] 126 126 138 137 145 145 145 137 89 89 ...
#   .. ..$ USAF       : int [1:19264] 163240 163240 163240 163240 163240 163240 163240 163240 163240 163240 ...
#   .. ..$ WBAN       : int [1:19264] 99999 99999 99999 99999 99999 99999 99999 99999 99999 99999 ...
#   .. ..$ DATE       : int [1:19264] 20130101 20130101 20130101 20130101 20130101 20130101 20130101 20130101 20130101 20130101 ...
#   .. ..$ TIME       : int [1:19264] 650 750 850 950 1050 1250 1350 1450 1550 1650 ...
#   .. ..$ DATASOURCE : int [1:19264] 4 4 4 4 4 4 4 4 4 4 ...
#   .. ..$ LAT        : num [1:19264] 40.5 40.5 40.5 40.5 40.5 ...
#   .. ..$ LON        : int [1:19264] 17400 17400 17400 17400 17400 17400 17400 17400 17400 17400 ...
#   .. ..$ CODE       : Factor w/ 2 levels "FM-15","FM-16": 1 1 1 1 1 1 1 1 1 1 ...
#   .. ..$ ELEV.DIM   : int [1:19264] 69 69 69 69 69 69 69 69 69 69 ...
#   .. ..$ CALL.LETTER: Factor w/ 2 levels "99999","LIBG ": 2 2 2 2 2 2 2 2 2 2 ...
#   .. ..$ QLT.CTRL   : Factor w/ 1 level "V020": 1 1 1 1 1 1 1 1 1 1 ...

# Table by years 
table(as.numeric(substr(grottaglie$station_data[[1]]$DATE, 1, 4)))
# 2013 2014 2015 2016 
# 4866 4816 4794 4788

我希望它可以帮助你。

注:如果在第一次运行时代码生成错误消息,请尝试重新运行它。