Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some similar R code #1

Open
geneorama opened this issue Jun 29, 2017 · 0 comments
Open

Some similar R code #1

geneorama opened this issue Jun 29, 2017 · 0 comments

Comments

@geneorama
Copy link
Member

geneorama commented Jun 29, 2017

This doesn't load the data into postgres, but I wrote a similar function for getting just one year at a time in R based on your code. I wanted something that doesn't download every year of the station's history every time it runs, I just need the year up to date.

I actually just need the past few days, but this is so fast that it doesn't matter.

# USAF='725340'  #  2. USAF code midway
# WBAN='14819'   #  3. WBAN code midway
# 
# USAF='725300'  #  2. USAF code ohare
# WBAN='94846'   #  3. WBAN code ohare

download_noaa_hourly <- function(usaf, wban, year){
    require(data.table) # You don't *need* to use data.table, but you'd be foolish not to.
    url <- sprintf("ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-lite/%s/%s-%s-%s.gz",
                   year, usaf, wban, year)
    dest <- file.path(tempdir(), basename(url))
    on.exit(unlink(dest)) ## delete temp file when exiting
    download.file(url, dest)
    result <- as.data.table(read.table(dest, na.strings = "-9999"))
    setnames(result, c("year", "month", "day", "hour", "air_temp_celsius", 
                       "dew_point_temp_celsius", "sea_level_pressure", 
                       "wind_direction", "wind_speed_km_hr", 
                       "sky_condition_total_coverage_code", 
                       "liquid_precipitation_mm_one_hour",
                       "liquid_precipitation_mm_six_hours"))
    return(result)
}
dat <- download_noaa_hourly(usaf="725340", wban="14819", year=2017)

This isn't an "issue", but fortunately you don't have complex contribution guidelines so you can't really yell at me. You can feel free to close without any action though.

Also, my first stab at a daily summary:

daily <- dat[i = TRUE, 
             j = list(max_temp = round(max(air_temp_celsius, na.rm = TRUE) / 10 * 9 / 5 + 32),
                      wind_ave = mean(wind_speed_km_hr, na.rm = TRUE),
                      wind_max = max(wind_speed_km_hr, na.rm = TRUE),
                      precip = sum(liquid_precipitation_mm_one_hour, na.rm = TRUE)),
             keyby = list(date = as.IDate(paste(year, month, day, sep="-")))]
daily
#            date max_temp wind_ave wind_max precip
#   1: 2017-01-01       40 25.78261       46      0
#   2: 2017-01-02       40 29.04167       41     18
#   3: 2017-01-03       42 33.04167       72      4
#   4: 2017-01-04       37 72.29167       98      0
#   5: 2017-01-05       16 55.91667       82     -3
# ---                                             
# 176: 2017-06-25       72 56.37500       88     -1
# 177: 2017-06-26       73 53.04167       93      0
# 178: 2017-06-27       76 33.79167       93      0
# 179: 2017-06-28       81 52.21739      103     44
# 180: 2017-06-29       80 64.14286       93      5
# 

edit: ohare was incorrectly labeled in comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant