-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadData.r
99 lines (90 loc) · 3.59 KB
/
loadData.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# A collection of functions to read formatted data files,
# generally obtained from the Bureau of Meteorology
# This includes station files, daily, half-hourly data files.
readDailyObs = function(obsfile, units = "m/s"){
# Read a file that contains daily maximum wind gust data.
# These files are described in DC02D_Notes_999999999425050.txt
# See /nas/cds/internal/hazard_events/archive/BOM_data/daily_max_wind_gust
#
# NOTES:
# Units of wind speed vary between versions of this data. Some cases
# have units of km/h, others m/s. The `units` argument indicates the
# units of the raw data, and the data are converted to m/s by default.
# Only handles conversion between m/s and km/h.
colclasses = c("character", "integer", "integer", "integer", "integer", "double",
"character", "double", "character", "character", "character", "character")
input <- read.csv(obsfile, skip = 1, header = FALSE,
strip.white = T, colClasses = colclasses)
ds_yr <- input$V3
ds_m <- input$V4
ds_d <- input$V5
ds_tm <- as.character(input$V10)
ds_tm[ds_tm == ""] = "0000"
dt = strptime(paste(ds_yr, ds_m, ds_d, ds_tm, sep = " "),
format = "%Y %m %d %H%M", tz = "GMT")
station = input$V2
if (units == "km/h") {
gust = input$V6 / 3.6
}else{
gust = input$V6
}
direction = input$V8
quality = input$V7
data <- data.frame(station = station, datetime = dt,
gust = gust, direction = direction,
quality = quality)
cbind(data)
}
readHalfHourlyData = function(obsfile, units="m/s"){
# Read a file that contains half-hourly observations.
# These files are described in HM01X_Notes_999999999425080.txt
# See /nas/cds/internal/hazard_events/archive/BOM_data/hourly_data
#
# NOTES:
# Units of wind speed vary between versions of this data. Some cases
# have units of km/h, others m/s. The `units` argument indicates the
# units of the raw data, and the data are converted to m/s by default.
# Only handles conversion between m/s and km/h.
#
# NOTES:
# 2017-09-08: Updated to read extended format files that include
# temperature, wet bulb temp and dew point temp
# observations.
input <- read.csv(obsfile, skip = 1, header = FALSE, strip.white = T)
ds_yr <- input$V8
ds_m <- input$V9
ds_d <- input$V10
ds_hr <- input$V11
ds_min <- input$V12
#build the LST datetime string:
dt <- ISOdatetime(ds_yr, ds_m, ds_d, ds_hr, ds_min, sec = 0, tz = "GMT")
station = input$V2
if (units == "km/h") {
gust = input$V25 / 3.6
}else{
gust = input$V25
}
direction = input$V26
quality = input$V22
data = data.frame(station = station, datetime = dt,
gust = gust, direction = direction,
quality = quality )
cbind(data)
}
readStationFile = function(stationfile, header = F, skip = 1){
# Read a formatted station file.
input = read.csv(stationfile, sep = ",", header = header, skip = skip, strip.white = T)
stnId = input$V2
stnName = input$V4
stnLat = input$V7
stnLon = input$V8
stnState = input$V10
stnOpenDate = strptime(paste("01", input$V5, sep = "/"), format = "%d/%m/%Y")
stnCloseDate = strptime(paste("01", input$V6, sep = "/"), format = "%d/%m/%Y")
stnDataStart = strptime(paste(input$V14, "01", "01", sep = "-"), format = "%Y-%m-%d")
stnDataEnd = strptime(paste(input$V15, "01", "01", sep = "-"), format = "%Y-%m-%d")
stations = data.frame(stnId, stnName, stnLat, stnLon,
stnState, stnOpenDate, stnCloseDate,
stnDataStart, stnDataEnd)
cbind(stations)
}