setwd('Desktop/DROUGHT/website/data')

# read in station ids
stlist = 'long_term_stns.list'
con <- file(stlist,"r")
stn <- readLines(con)
close(con)

n = length(stn)

# read in monthly stats for every station
filename = 'monthly_histories.txt'
con <- file(filename,"r")
var <- readLines(con)
close(con)

# define new station lines
b = which(substr(var,1,4) == "name")
###between b[1] and b[2] is the first station's data

for (i in 1:n) {
  sname = substr(var[b[i]],7,nchar(var[b[i]])-1)
  
  #grab n lines for each station
  sline = b[i] + 1
  if(i < n) { eline = b[i+1] - 1 }
  if(i == n) { eline = length(var) }
  
  por = eline - sline + 1
  dates = array(data=NA, dim=por)
  maxt = dates; mint = dates;
  pcpn = dates; snow = dates;
  
  #place data in arrays
  k=1
  for (j in sline:eline) {
    cvar = unlist(strsplit(var[j],","))
    
    cvar[cvar == "M"] <- NA
    cvar[cvar == "T"] = 0.001
    
    dates[k] = cvar[1]
    maxt[k] = as.numeric(cvar[2])
    mint[k] = as.numeric(cvar[3])
    pcpn[k] = as.numeric(cvar[4])
    snow[k] = as.numeric(cvar[5])
    
    k=k+1
  }
  
  #calculate annual and monthly statistics
  maxtmax = array(data=NA, dim=13)
  maxtmin = maxtmax; maxtmean = maxtmax;  
  mintmax = maxtmax; mintmin = maxtmax; mintmean = maxtmax;
  pcpnmax = maxtmax; pcpnmin = maxtmax; pcpnmean = maxtmax;
  ##snowmax = maxtmax; snowmean = maxtmax;
    
  ##monthly
  mos = substr(dates,6,7)
  for (j in 1:12) {
    cmo = which(as.numeric(mos) == j)
    
    maxtmax[j] = max(maxt[cmo], na.rm=TRUE)
    maxtmin[j] = min(maxt[cmo], na.rm=TRUE)
    maxtmean[j] = mean(maxt[cmo], na.rm=TRUE)
  
    mintmax[j] = max(mint[cmo], na.rm=TRUE)
    mintmin[j] = min(mint[cmo], na.rm=TRUE)
    mintmean[j] = mean(mint[cmo], na.rm=TRUE)

    pcpnmax[j] = max(pcpn[cmo], na.rm=TRUE)
    pcpnmin[j] = min(pcpn[cmo], na.rm=TRUE)
    pcpnmean[j] = mean(pcpn[cmo], na.rm=TRUE)
  }

  ##annual
  yrs = substr(dates,1,4)
  syr = as.numeric(yrs[1]); eyr = as.numeric(yrs[por]);
  years = seq(syr,eyr,1); nyr = length(years);
  
  annvar = array(data=NA, dim=c(nyr,3))
  
  for (j in 1:nyr) {
    cyr = which(yrs == years[j])

    ## are there 12 full months to use the calculation?
    if(length(cyr) == 12) {
      annvar[j,1] = mean(maxt[cyr])
      annvar[j,2] = mean(mint[cyr])
      annvar[j,3] = sum(pcpn[cyr])
    } 
  }
  
  maxtmax[13] = max(annvar[,1], na.rm=TRUE)
  maxtmin[13] = min(annvar[,1], na.rm=TRUE)
  maxtmean[13] = mean(annvar[,1], na.rm=TRUE)
  
  mintmax[13] = max(annvar[,2], na.rm=TRUE)
  mintmin[13] = min(annvar[,2], na.rm=TRUE)
  mintmean[13] = mean(annvar[,2], na.rm=TRUE)
  
  pcpnmax[13] = max(annvar[,3], na.rm=TRUE)
  pcpnmin[13] = min(annvar[,3], na.rm=TRUE)
  pcpnmean[13] = mean(annvar[,3], na.rm=TRUE)
  
  ### output statistics to a text table
  blank = ' <br>'
  annhead = '********** ANNUAL STATISTICS **********<br>'
  monhead = '********** MONTHLY STATISTICS *********<br>'
  
  maxthead = '<font size=-1>MAXIMUM TEMPERATURE</font><br>'
  minthead = '<font size=-1>MINIMUM TEMPERATURE</font><br>'
  pcpnhead = '<font size=-1>PRECIPITATION</font><br>'
  
  avelabel = 'average: '
  hilabel = 'record hi: '
  lolabel = 'record lo: '
  
  smos = c('<b>JANUARY</b><br>','<b>FEBRUARY</b><br>','<b>MARCH</b><br>','<b>APRIL</b><br>','<b>MAY</b><br>','<b>JUNE</b><br>','<b>JULY</b><br>','<b>AUGUST</b><br>','<b>SEPTEMBER</b><br>','<b>OCTOBER</b><br>','<b>NOVEMBER</b><br>','<b>DECEMBER</b><br>')
  
  wfile <- paste('hist/stn_hist_',stn[i],'.htm', sep="")
  outfile <- file(wfile, "w")

  write(paste(sname,'<br>',sep=""), file=outfile, append=TRUE)
  write(paste(stn[i],'<br>',sep=""), file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
  
  write(annhead, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)

  write(maxthead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(maxtmean[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(maxtmax[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(maxtmin[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
  
  write(minthead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(mintmean[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(mintmax[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(mintmin[13],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
 
  write(pcpnhead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(pcpnmean[13],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(pcpnmax[13],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(pcpnmin[13],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
  
  write(monhead, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
  
  for (j in 1:12) {
    write(smos[j], file=outfile, append=TRUE)
    
  write(maxthead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(maxtmean[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(maxtmax[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(maxtmin[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
  
  write(minthead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(mintmean[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(mintmax[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(mintmin[j],digits=1),'F<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
 
  write(pcpnhead, file=outfile, append=TRUE)
  sout = paste(avelabel,round(pcpnmean[j],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(hilabel,round(pcpnmax[j],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  sout = paste(lolabel,round(pcpnmin[j],digits=2),' in.<br>', sep="")
  write(sout, file=outfile, append=TRUE)
  write(blank, file=outfile, append=TRUE)
    
  }


  close(outfile)

}
    
    
 stlist = 'long_term_stns.csv'
con <- file(stlist,"r")
dat <- readLines(con)
close(con)
   
  wfile <- 'htmlcode.txt'
  outfile <- file(wfile, "w")
  
  for(i in 2:length(dat)) {
  cvar = unlist(strsplit(dat[i],","))
  
  out1 = '<option value="data/hist/stn_hist_'
  out2 = '.txt">'
  out3 = '</option>'
  out4 = ' '
  
  out5 = paste(out1,cvar[1],out2,cvar[1],out4,cvar[2],out3, sep="")

  write(out5, file=outfile, append=TRUE)

  } 
    
 close(outfile)   
    
    
