pro MakeKeyFile, stnFile, startYr, keyFile
  if n_elements(keyFile) eq 0 then keyFile='full.key'

  openr, un, /get, stnFile
  openw, oun, /get, keyFile

  line=''

;; Skip the header
  readf, un, line &  readf, un, line

  for i=0,n_elements(startYr)-1 do begin
    if eof(un) then break

    readf, un, line
;    if startYr(i) ne -99 then begin

; read info from this line of the file
      sname=strcompress(strmid(line, 16,29), /remove_all)
      lat=strsplit(strmid(line, 136,6),':',  /extract)
      lon=strsplit(strmid(line, 148,9),':',  /extract)

;Calculate latitude and Longitude, assumes we are in the North-West
;hemisphere
      lat = 100*fix(lat(0)) + fix(lat(1))
      lon = 100*fix(lon(0)) - fix(lon(1))
; Write output to file
      printf, oun, sname+'.wth', lat, lon, startYr(i)
;    endif

  endfor
  close, oun, un
  free_lun, oun, un
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  Read the first data segment, counting the number of lines
;;  necessary for an entire data segment and moving the file pointer
;;  to the beginning of the next data segment.  Then return the data
;;  and year for the first segment in the named variables given
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro readFirst, un, maxT, minT, prec, name, year, n_lines
  maxT=replicate(-99.99,12)
  minT=replicate(-99.99,12)
  prec=replicate(-99.99,12)
  point_lun, (-1*un),location
  line=''
;  count=0
;  n_lines=intarr(4)
  dataDex = (indgen(12)*5) + 11

  readf, un, line
  line=strsplit(line, ',', /extract)
  name=strcompress(line(2),/remove_all)
  year=fix(line(6))
  cur_year=year

  type=''
  point_lun, un, location

;  while not strcmp(starttype,type) and (year eq cur_year) and not eof(un) do begin
  while (year eq cur_year) and not eof(un) do begin
    line=''
    readf, un, line
    line=strsplit(line, ',', /extract)
    type=line(4)
    nextname=strcompress(line(2),/remove_all)
    cur_year=fix(line(6))
;    print, nextname, cur_year
;    count=count+1

    if year eq cur_year then begin
      point_lun, (-1*un),location
      case type of 
        'MMNT': begin
          minT = (float(line(dataDex)) -320)/18 ;tenths of degrees F to 
;        n_lines(0)=count
        end
        'MMXT': begin
          maxT = (float(line(dataDex)) -320)/18 ; degrees C
;        n_lines(1)=count
        end
        'TPCP': begin
          prec = float(line(dataDex))  * 0.0254 ;0.01in = 0.0254 cm
;        n_lines(2)=count
        end
        else:
      endcase
    endif

;    print, '--------------------------------'
  endwhile

  badDex = where(minT lt -5000)
  if badDex(0) ne -1 then $ 
    minT(badDex) = -99.99
  badDex = where(maxT lt -5000)
  if badDex(0) ne -1 then $ 
    maxT(badDex) = -99.99
  badDex = where(prec lt -2000)
  if badDex(0) ne -1 then $ 
    prec(badDex) = -99.99
  
;  n_lines(3)=count
;; reset the location to the previous line
  point_lun, un, location
  
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Similar to read first, but now we know how many lines there are,
;; and which lines contain the information we want.  
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro readData, un, maxT, minT, prec, name, year, n_lines
  line=''
  dataDex = (indgen(12)*5) + 11
  segment=strarr(n_lines(3))

;;read an entire segment of data from the file  
  for i=0, n_lines(3)-1 do begin
    readf, un, line
    segment(i)=line
  endfor
  
;;find some basic info from the last line
  line=strsplit(line, ',', /extract)
  name=strcompress(line(2),/remove_all)
  year=fix(line(6))
  
;; now we get the info we really care about.  the first three elements
;; of n_lines are indices into the segment.  
  line=strsplit(segment(n_lines(0)),',', /extract)
  minT = (float(line(dataDex)) -320)/18 ;tenths of degrees F to 

  line=strsplit(segment(n_lines(1)),',', /extract)
  maxT = (float(line(dataDex)) -320)/18 ; degrees C

  line=strsplit(segment(n_lines(2)),',', /extract)
  prec = float(line(dataDex))  * 0.0254 ;0.01in = 0.0254 cm

;-5573.28 = -99999 converted to celsius ;) 
  badDex = where(minT lt -5000)
  if badDex(0) ne -1 then $ 
    minT(badDex) = -99.99
  badDex = where(maxT lt -5000)
  if badDex(0) ne -1 then $ 
    maxT(badDex) = -99.99
;; similarly -2539 is -99999 converted to cm
  badDex = where(prec lt -2000)
  if badDex(0) ne -1 then $ 
    prec(badDex) = -99.99
  
end
    
;;;;;;;;;;;;;;;;;;;;;;
;;
;; write a century .wth file based on the data collected.  
;;
;;;;;;;;;;;;;;;;;;;;;;
pro writeWTHfile, fname, tMax, tMin, prec, startyr
  nyears=2003-startyr
  help, prec
  openw, oun, /get, fname
  for i=0, nyears-1 do begin
    printf, oun, 'prec  '+strcompress(string(startyr+i),/remove_all) $
            + strjoin(string(format='(F7.2)', prec(*,i)))
    printf, oun, 'tmax  '+strcompress(string(startyr+i),/remove_all) $
            + strjoin(string(format='(F7.2)', tMax(*,i)))
    printf, oun, 'tmin  '+strcompress(string(startyr+i),/remove_all) $
            + strjoin(string(format='(F7.2)', tMin(*,i)))
  endfor

  close, oun
  free_lun, oun
end
  

pro toCent, datFile, stnFile, keyFile=keyFile
  if n_elements(datFile) eq 0 then begin
    datFile=(findfile('*dat.txt'))(0)
    stnFile=(findfile('*stn.txt'))(0)
  endif else if n_elements(stnFile) eq 0 then begin
    stnFile=(findfile('*stn.txt'))(0)
  endif

  startyr=0
  openr, un, /get, datFile
  
  line=''
  readf, un, line & readf, un, line
  
  readFirst, un, maxT, minT, prec, new_name, year, n_lines
  while not eof(un) do begin
    
    station_name=new_name
;    print, '*********************** '+new_name+' *********************************'
    i=0
    
    readFirst, un, new_maxT, new_minT, new_prec, new_name, new_year, n_lines
    while strcmp(new_name,station_name) and not eof(un) do begin
      
      while new_year gt year(i)+1 do begin
        print, 'missing year!', new_year, year(i)
        maxT=[[maxT],[replicate(-99.99,12)]]
        minT=[[minT],[replicate(-99.99,12)]]
        prec=[[prec],[replicate(-99.99,12)]]
        year=[year,year(i)+1]
        i=i+1
      endwhile
      
      if new_year gt 1998 and new_year lt 2002 then begin
        if n_elements(where(new_maxT eq -99.99)) gt 1 or $
          n_elements(where(new_minT eq -99.99)) gt 1 or $
          n_elements(where(new_prec eq -99.99)) gt 1 then $
          
          print, 'remember to uncomment the following line!!'
;          new_year=-99
      endif
      
      maxT=[[maxT],[new_maxT]]
      minT=[[minT],[new_minT]]
      prec=[[prec],[new_prec]]
      year=[year,new_year]
      i=i+1
      
      readFirst, un, new_maxT, new_minT, new_prec, new_name, new_year, n_lines

    endwhile
    print,' exit ',station_name
    index=where(year eq -99) ;;this is our error check
;    if index(0) eq -1 and $
;      n_elements(year) gt 20 and $
;      year(n_elements(year)-1) eq 2002 then begin
      
      writeWTHfile, station_name+'.wth', maxT, minT, prec, year(0)
      startyr = [startyr,year(0)]
      
;    endif else $
;      startyr=[startyr,-99]
;; set up the next iteration
    maxT=new_maxT
    minT=new_minT
    prec=new_prec
    year=new_year
  endwhile  

  MakeKeyfile, stnFile, startyr, keyFile
end
