This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
isdparser.Rmd
374 lines (320 loc) · 17.5 KB
/
isdparser.Rmd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
---
title: "Introduction to the isdparser package"
author: "Scott Chamberlain"
date: "2020-01-31"
output:
html_document:
toc: true
toc_float: true
vignette: >
%\VignetteIndexEntry{Introduction to the isdparser package}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
`isdparser` is an parser for ISD/ISD NOAA files
Code liberated from `rnoaa` to focus on ISD parsing since it's sorta
complicated. Has minimal dependencies, so you can parse your ISD/ISH
files without needing the deps that `rnoaa` needs. Will be used by
`rnoaa` once on CRAN.
Documentation at ftp://ftp.ncdc.noaa.gov/pub/data/noaa/ish-format-document.pdf
Package API:
* `isd_parse()` - parse all lines in a file, with parallel option
* `isd_parse_line()` - parse a single line - you choose which lines to parse
and how to apply the function to your lines
* `isd_transform()` - transform ISD data variables
* `isd_parse_csv()` - parse csv format files
`isd_parse_csv()` parses NOAA ISD csv files, whereas `isd_parse()` and `isd_parse_line()`
both handle compressed files where each row of data is a string that needs to be parsed.
`isd_parse_csv()` is faster than `isd_parse()` because parsing each line takes some
time - although using `isd_parse(parallel = TRUE)` option gets closer to the speed of
`isd_parse_csv()`.
## Install
Stable from CRAN
```r
install.packages("isdparser")
```
Dev version
```r
remotes::install_github("ropensci/isdparser")
```
```r
library("isdparser")
```
## isd_parse_csv: parse a CSV file
Using a csv file included in the package:
```r
path <- system.file('extdata/00702699999.csv', package = "isdparser")
isd_parse_csv(path)
#> # A tibble: 6,843 x 68
#> station date source latitude longitude elevation name
#> <int> <dttm> <int> <dbl> <dbl> <dbl> <chr>
#> 1 7.03e8 2017-02-10 14:04:00 4 0 0 7026 WXPO…
#> 2 7.03e8 2017-02-10 14:14:00 4 0 0 7026 WXPO…
#> 3 7.03e8 2017-02-10 14:19:00 4 0 0 7026 WXPO…
#> 4 7.03e8 2017-02-10 14:24:00 4 0 0 7026 WXPO…
#> 5 7.03e8 2017-02-10 14:29:00 4 0 0 7026 WXPO…
#> 6 7.03e8 2017-02-10 14:34:00 4 0 0 7026 WXPO…
#> 7 7.03e8 2017-02-10 14:39:00 4 0 0 7026 WXPO…
#> 8 7.03e8 2017-02-10 14:44:00 4 0 0 7026 WXPO…
#> 9 7.03e8 2017-02-10 14:49:00 4 0 0 7026 WXPO…
#> 10 7.03e8 2017-02-10 14:54:00 4 0 0 7026 WXPO…
#> # … with 6,833 more rows, and 61 more variables: report_type <chr>,
#> # call_sign <int>, quality_control <chr>, wnd <chr>, cig <chr>, vis <chr>,
#> # tmp <chr>, dew <chr>, slp <chr>, wind_direction <chr>,
#> # wind_direction_quality <chr>, wind_code <chr>, wind_speed <chr>,
#> # wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>, automated_atmospheric_condition_code <chr>,
#> # quality_automated_atmospheric_condition_code <chr>, coverage_code <chr>,
#> # coverage_quality_code <chr>, base_height_dimension <chr>,
#> # base_height_quality_code <chr>, cloud_type_code <chr>,
#> # cloud_type_quality_code <chr>, connective_cloud_attribute <chr>,
#> # vertical_datum_attribute <chr>, base_height_upper_range_attribute <chr>,
#> # base_height_lower_range_attribute <chr>, coverage <chr>,
#> # opaque_coverage <chr>, coverage_quality <chr>, lowest_cover <chr>,
#> # lowest_cover_quality <chr>, low_cloud_genus <chr>,
#> # low_cloud_genus_quality <chr>, lowest_cloud_base_height <chr>,
#> # lowest_cloud_base_height_quality <chr>, mid_cloud_genus <chr>,
#> # mid_cloud_genus_quality <chr>, high_cloud_genus <chr>,
#> # high_cloud_genus_quality <chr>, altimeter_setting_rate <chr>,
#> # altimeter_quality_code <chr>, station_pressure_rate <chr>,
#> # station_pressure_quality_code <chr>, speed_rate <chr>, quality_code <chr>,
#> # rem <chr>, eqd <chr>
```
Download a file first:
```r
path <- file.path(tempdir(), "00702699999.csv")
x <- "https://www.ncei.noaa.gov/data/global-hourly/access/2017/00702699999.csv"
download.file(x, path)
isd_parse_csv(path)
#> # A tibble: 6,843 x 68
#> station date source latitude longitude elevation name
#> <int> <dttm> <int> <dbl> <dbl> <dbl> <chr>
#> 1 7.03e8 2017-02-10 14:04:00 4 0 0 7026 WXPO…
#> 2 7.03e8 2017-02-10 14:14:00 4 0 0 7026 WXPO…
#> 3 7.03e8 2017-02-10 14:19:00 4 0 0 7026 WXPO…
#> 4 7.03e8 2017-02-10 14:24:00 4 0 0 7026 WXPO…
#> 5 7.03e8 2017-02-10 14:29:00 4 0 0 7026 WXPO…
#> 6 7.03e8 2017-02-10 14:34:00 4 0 0 7026 WXPO…
#> 7 7.03e8 2017-02-10 14:39:00 4 0 0 7026 WXPO…
#> 8 7.03e8 2017-02-10 14:44:00 4 0 0 7026 WXPO…
#> 9 7.03e8 2017-02-10 14:49:00 4 0 0 7026 WXPO…
#> 10 7.03e8 2017-02-10 14:54:00 4 0 0 7026 WXPO…
#> # … with 6,833 more rows, and 61 more variables: report_type <chr>,
#> # call_sign <int>, quality_control <chr>, wnd <chr>, cig <chr>, vis <chr>,
#> # tmp <chr>, dew <chr>, slp <chr>, wind_direction <chr>,
#> # wind_direction_quality <chr>, wind_code <chr>, wind_speed <chr>,
#> # wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>, automated_atmospheric_condition_code <chr>,
#> # quality_automated_atmospheric_condition_code <chr>, coverage_code <chr>,
#> # coverage_quality_code <chr>, base_height_dimension <chr>,
#> # base_height_quality_code <chr>, cloud_type_code <chr>,
#> # cloud_type_quality_code <chr>, connective_cloud_attribute <chr>,
#> # vertical_datum_attribute <chr>, base_height_upper_range_attribute <chr>,
#> # base_height_lower_range_attribute <chr>, coverage <chr>,
#> # opaque_coverage <chr>, coverage_quality <chr>, lowest_cover <chr>,
#> # lowest_cover_quality <chr>, low_cloud_genus <chr>,
#> # low_cloud_genus_quality <chr>, lowest_cloud_base_height <chr>,
#> # lowest_cloud_base_height_quality <chr>, mid_cloud_genus <chr>,
#> # mid_cloud_genus_quality <chr>, high_cloud_genus <chr>,
#> # high_cloud_genus_quality <chr>, altimeter_setting_rate <chr>,
#> # altimeter_quality_code <chr>, station_pressure_rate <chr>,
#> # station_pressure_quality_code <chr>, speed_rate <chr>, quality_code <chr>,
#> # rem <chr>, eqd <chr>
```
## isd_parse_line: parse lines from an ASCII strings file
```r
path <- system.file('extdata/024130-99999-2016.gz', package = "isdparser")
lns <- readLines(path, encoding = "latin1")
isd_parse_line(lns[1])
#> # A tibble: 1 x 38
#> total_chars usaf_station wban_station date time date_flag latitude longitude
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 0054 024130 99999 2016… 0000 4 +60750 +012767
#> # … with 30 more variables: type_code <chr>, elevation <chr>,
#> # call_letter <chr>, quality <chr>, wind_direction <chr>,
#> # wind_direction_quality <chr>, wind_code <chr>, wind_speed <chr>,
#> # wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>,
#> # AW1_present_weather_observation_identifier <chr>,
#> # AW1_automated_atmospheric_condition_code <chr>,
#> # AW1_quality_automated_atmospheric_condition_code <chr>, REM_remarks <chr>,
#> # REM_identifier <chr>, REM_length_quantity <chr>, REM_comment <chr>
```
Or, give back a list
```r
head(
isd_parse_line(lns[1], as_data_frame = FALSE)
)
#> $total_chars
#> [1] "0054"
#>
#> $usaf_station
#> [1] "024130"
#>
#> $wban_station
#> [1] "99999"
#>
#> $date
#> [1] "20160101"
#>
#> $time
#> [1] "0000"
#>
#> $date_flag
#> [1] "4"
```
Optionally don't include "Additional" and "Remarks" sections in parsed output.
```r
isd_parse_line(lns[1], additional = FALSE)
#> # A tibble: 1 x 31
#> total_chars usaf_station wban_station date time date_flag latitude longitude
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 0054 024130 99999 2016… 0000 4 +60750 +012767
#> # … with 23 more variables: type_code <chr>, elevation <chr>,
#> # call_letter <chr>, quality <chr>, wind_direction <chr>,
#> # wind_direction_quality <chr>, wind_code <chr>, wind_speed <chr>,
#> # wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>
```
## isd_parse: parse an ASCII strings file
Downloading a new file
```r
path <- file.path(tempdir(), "007026-99999-2017.gz")
y <- "ftp://ftp.ncdc.noaa.gov/pub/data/noaa/2017/007026-99999-2017.gz"
download.file(y, path)
isd_parse(path)
#> # A tibble: 6,843 x 72
#> total_chars usaf_station wban_station date time date_flag latitude
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 0157 007026 99999 2017… 1404 4 +00000
#> 2 0157 007026 99999 2017… 1414 4 +00000
#> 3 0157 007026 99999 2017… 1419 4 +00000
#> 4 0157 007026 99999 2017… 1424 4 +00000
#> 5 0157 007026 99999 2017… 1429 4 +00000
#> 6 0144 007026 99999 2017… 1434 4 +00000
#> 7 0157 007026 99999 2017… 1439 4 +00000
#> 8 0157 007026 99999 2017… 1444 4 +00000
#> 9 0172 007026 99999 2017… 1449 4 +00000
#> 10 0157 007026 99999 2017… 1454 4 +00000
#> # … with 6,833 more rows, and 65 more variables: longitude <chr>,
#> # type_code <chr>, elevation <chr>, call_letter <chr>, quality <chr>,
#> # wind_direction <chr>, wind_direction_quality <chr>, wind_code <chr>,
#> # wind_speed <chr>, wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>, GF1_sky_condition <chr>, GF1_coverage <chr>,
#> # GF1_opaque_coverage <chr>, GF1_coverage_quality <chr>,
#> # GF1_lowest_cover <chr>, GF1_lowest_cover_quality <chr>,
#> # GF1_low_cloud_genus <chr>, GF1_low_cloud_genus_quality <chr>,
#> # GF1_lowest_cloud_base_height <chr>,
#> # GF1_lowest_cloud_base_height_quality <chr>, GF1_mid_cloud_genus <chr>,
#> # GF1_mid_cloud_genus_quality <chr>, GF1_high_cloud_genus <chr>,
#> # GF1_high_cloud_genus_quality <chr>, MA1_atmospheric_pressure <chr>,
#> # MA1_altimeter_setting_rate <chr>, MA1_altimeter_quality_code <chr>,
#> # MA1_station_pressure_rate <chr>, MA1_station_pressure_quality_code <chr>,
#> # REM_remarks <chr>, REM_identifier <chr>, REM_length_quantity <chr>,
#> # REM_comment <chr>, OC1_wind_gust_observation_identifier <chr>,
#> # OC1_speed_rate <chr>, OC1_quality_code <chr>,
#> # GA1_sky_cover_layer_identifier <chr>, GA1_coverage_code <chr>,
#> # GA1_coverage_quality_code <chr>, GA1_base_height_dimension <chr>,
#> # GA1_base_height_quality_code <chr>, GA1_cloud_type_code <chr>,
#> # GA1_cloud_type_quality_code <chr>, GE1_sky_condition <chr>,
#> # GE1_connective_cloud_attribute <chr>, GE1_vertical_datum_attribute <chr>,
#> # GE1_base_height_upper_range_attribute <chr>,
#> # GE1_base_height_lower_range_attribute <chr>,
#> # AW1_present_weather_observation_identifier <chr>,
#> # AW1_automated_atmospheric_condition_code <chr>,
#> # AW1_quality_automated_atmospheric_condition_code <chr>
```
### Parallel
```r
isd_parse(path, parallel = TRUE)
```
### Progress
> note: Progress not printed if `parallel = TRUE`
```r
isd_parse(path, progress = TRUE)
#>
#> |========================================================================================| 100%
#> # A tibble: 2,601 × 42
#> total_chars usaf_station wban_station date time date_flag latitude longitude type_code
#> <dbl> <chr> <chr> <date> <chr> <chr> <dbl> <dbl> <chr>
#> 1 54 024130 99999 2016-01-01 0000 4 60.75 12.767 FM-12
#> 2 54 024130 99999 2016-01-01 0100 4 60.75 12.767 FM-12
#> 3 54 024130 99999 2016-01-01 0200 4 60.75 12.767 FM-12
#> 4 54 024130 99999 2016-01-01 0300 4 60.75 12.767 FM-12
#> 5 54 024130 99999 2016-01-01 0400 4 60.75 12.767 FM-12
#> 6 39 024130 99999 2016-01-01 0500 4 60.75 12.767 FM-12
#> 7 54 024130 99999 2016-01-01 0600 4 60.75 12.767 FM-12
#> 8 39 024130 99999 2016-01-01 0700 4 60.75 12.767 FM-12
#> 9 54 024130 99999 2016-01-01 0800 4 60.75 12.767 FM-12
#> 10 54 024130 99999 2016-01-01 0900 4 60.75 12.767 FM-12
#> # ... with 2,591 more rows, and 33 more variables: elevation <dbl>, call_letter <chr>, quality <chr>,
#> # wind_direction <dbl>, wind_direction_quality <chr>, wind_code <chr>, wind_speed <dbl>,
#> # wind_speed_quality <chr>, ceiling_height <chr>, ceiling_height_quality <chr>,
#> # ceiling_height_determination <chr>, ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>, visibility_code_quality <chr>,
#> # temperature <dbl>, temperature_quality <chr>, temperature_dewpoint <dbl>,
#> # temperature_dewpoint_quality <chr>, air_pressure <dbl>, air_pressure_quality <chr>,
#> # AW1_present_weather_observation_identifier <chr>, AW1_automated_atmospheric_condition_code <chr>,
#> # AW1_quality_automated_atmospheric_condition_code <chr>, N03_original_observation <chr>,
#> # N03_original_value_text <chr>, N03_units_code <chr>, N03_parameter_code <chr>, REM_remarks <chr>,
#> # REM_identifier <chr>, REM_length_quantity <chr>, REM_comment <chr>
```
### Additional data
Optionally don't include "Additional" and "Remarks" sections in parsed output.
```r
isd_parse(path, additional = FALSE)
#> # A tibble: 6,843 x 31
#> total_chars usaf_station wban_station date time date_flag latitude
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 0157 007026 99999 2017… 1404 4 +00000
#> 2 0157 007026 99999 2017… 1414 4 +00000
#> 3 0157 007026 99999 2017… 1419 4 +00000
#> 4 0157 007026 99999 2017… 1424 4 +00000
#> 5 0157 007026 99999 2017… 1429 4 +00000
#> 6 0144 007026 99999 2017… 1434 4 +00000
#> 7 0157 007026 99999 2017… 1439 4 +00000
#> 8 0157 007026 99999 2017… 1444 4 +00000
#> 9 0172 007026 99999 2017… 1449 4 +00000
#> 10 0157 007026 99999 2017… 1454 4 +00000
#> # … with 6,833 more rows, and 24 more variables: longitude <chr>,
#> # type_code <chr>, elevation <chr>, call_letter <chr>, quality <chr>,
#> # wind_direction <chr>, wind_direction_quality <chr>, wind_code <chr>,
#> # wind_speed <chr>, wind_speed_quality <chr>, ceiling_height <chr>,
#> # ceiling_height_quality <chr>, ceiling_height_determination <chr>,
#> # ceiling_height_cavok <chr>, visibility_distance <chr>,
#> # visibility_distance_quality <chr>, visibility_code <chr>,
#> # visibility_code_quality <chr>, temperature <chr>,
#> # temperature_quality <chr>, temperature_dewpoint <chr>,
#> # temperature_dewpoint_quality <chr>, air_pressure <chr>,
#> # air_pressure_quality <chr>
```