      program tminmax
      implicit none

c -----
c number of station
      integer NOBS
      parameter (NOBS=25)

c number of data
      integer NDAT
      parameter (NDAT=121)

c missing value
      real    MISS
      parameter (MISS=-999)

c IO
      integer IFILE
      parameter (IFILE=21)
      integer O1FILE
      parameter (O1FILE=22)
      integer O2FILE
      parameter (O2FILE=23)

c -----
c station position
      real    x(NOBS), y(NOBS)

c time
      character*8  HHMMSS(NDAT)
c data
      real    t, wd, ws

c minimum and maximum temperature
      real    tmin(NOBS), tmax(NOBS)

c loop control
      integer i, j

c -----
c read station position
      open(IFILE,file='location.txt')
      do 100 j = 1, NOBS
        read(IFILE,*) x(j), y(j)
 100  continue
      close(IFILE)

c find minimum and maximum
      open(IFILE,file='datafile')
      j = 0
 220  continue
      j = j + 1
      read(IFILE,*,end=240)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      tmin(j) = MISS
      tmax(j) = MISS
      do 230 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (t.ne.MISS) then
            if (tmin(j).eq.MISS) then
                tmin(j) = t
              else if (t.lt.tmin(j)) then
                tmin(j) = t
            end if
            if (tmax(j).eq.MISS) then
                tmax(j) = t
              else if (t.gt.tmax(j)) then
                tmax(j) = t
            end if
        end if
 230  continue
      go to 220
 240  continue
      close(IFILE)

c write time of minimum and maximum temperature
      open(IFILE,file='datafile')
      open(O1FILE,file='minfile.csv')
      open(O2FILE,file='maxfile.csv')
      j = 0
 320  continue
      j = j + 1
      read(IFILE,*,end=340)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 330 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (t.le.tmin(j)) then
            write(O1FILE,*) x(j), ',', y(j), ',', HHMMSS(i), t
        end if
        if (t.ge.tmax(j)) then
            write(O2FILE,*) x(j), ',', y(j), ',', HHMMSS(i), t
        end if
 330  continue
      go to 320
 340  continue
      close(IFILE)
      close(O1FILE)
      close(O2FILE)


      stop
      end
