---
title: "R Notebook"
output: html_notebook
---

```{r typhoons, eval=F}
noaa <- readLines('http://www.ssd.noaa.gov/PS/TROP/2018/adt/archive.html')
library(stringr)
library(readr)
library(dplyr)
pattern = 'http[^\"]+text[^\"]+\\.txt'
urls <- str_extract(noaa,pattern)
urls <- urls[!is.na(urls)]
header1 <- c("date", "time", "intensity_ci", "intensity_mslp", "intensity_vmax","tno_fnl","tno_adj","tno_ini","constrnt","wkng","rpd","temp_ctr","temp_mean","scene","est_rmw","mw","lat","lon","mthd","sat","vza","comments")
widths1 <- c(9,7,5,7,6,5,4,4,10,5,5,8,7,8,7,6,8,8,6,9,5,NA)
header2 = header <- c("date", "time", "intensity_ci", "intensity_mslp", "intensity_mslpplat", "intensity_vmax","tno_fnl","tno_adj","tno_ini","constrnt","wkng","rpd","temp_ctr","temp_mean","scene","est_rmw","mw","lat","lon","mthd","sat","vza","comments")
widths2 <- c(9,7,5,7,8,6,5,4,4,10,5,5,8,7,8,7,6,8,8,6,9,5,NA)
first <- T
q <- 1
# Works as of September 8, 2019 -- may be broken by file updates
for(url in urls) {
  print(q)
  q <- q + 1
  lines <- readLines(url)
  name <- strsplit(substr(url,52,10000), "-")[[1]][1]
  # check if "BiasAdj" in line 4 -- if so, need an extra column!
  if(length(grep("BiasAdj",lines))) {
    widths <- widths2
    h <- header2
  } else {
    widths <- widths1
    h <- header1
  }
  
  if(url=="http://www.ssd.noaa.gov/PS/TROP/DATA/2018/adt/text/17P-list.txt") {
    # Special rule for this file due to incorrect negative MSLP values offsetting columns
    for(i in 5:55) {
      lines[i] = paste0(substr(lines[i],1,22), substr(lines[i],24,nchar(lines[i])), sep='')
    }
  }
  lines <- lines[5:(length(lines)-3)]
  
  data <- read_fwf(paste0(lines,collapse="\n"), fwf_widths(widths, col_names=h), na="N/A")
  data$est_rmw <- as.character(data$est_rmw)
  data$typhoon_name <- name
  if(first) {
    first = F
    typhoon <- data
  } else {
    typhoon <- bind_rows(typhoon, data)
  }
}
write.csv(typhoon,"typhoon.csv", row.names=F)
```
