FORTRAN I/O Samples

Unsigned byte data in a two dimensional array

      program readplds

      parameter (width=720, height = 340)

      character*1 lrec(width)

      integer col, row, rlen, snow(width,height), x, y

C     record length is specified in words not bytes
      rlen = width / 4

C     open file
      open(unit=1, file='mthly_snow_198012.plds.new', 
     1     form='unformatted', access='direct', recl=rlen)

C     read header and display
      read(1) lrec
      write(*,*) lrec

      do row = 1, height
        read(1) lrec

        do col = 1, width
          value = ichar(lrec(col))
          if (value .lt. 0) value = value + 256
          snow(col, row) = value
        end do

      end do

      close(1)

C     query grid
 100  continue
      print *, 'enter x y from rdpix'
      read *, x, y
      col = x + 1
      row = height - y
      print '(a,i4,a,i4,a,i4)', 
     1     'col=', col, ', row=', row, ', value=', snow(col,row)
      goto 100

      end


Unsigned byte data in a two dimensional array with a 300 byte header

      program readgts
      implicit none

C sample program to read Goddard Time Series data
C data file has 300 byte header followed by 304x448 byte array

      integer ival, irec, lbyte, width, height, hdrsiz
      parameter(width=304,height=448,hdrsiz=300)
      character*1 header(hdrsiz)
      character*1 logrec(4)
      integer values(width*height)

C open file
C recl=4 is the largest common denominator between 300 and 304
C on some systems recl is specified in words, in which case recl=1
      open(unit=1,file='199701.N13',form='unformatted',
     1    access='direct', recl=4)

C read 4 byte logical records and pack them into header and data arrays

C read header and display
      ival = 1
      do irec=1,hdrsiz/4
        read(1,rec=irec) logrec
        do lbyte=1,4
          header(ival) = logrec(lbyte)
          ival = ival + 1
        enddo
      enddo

      write(*,*) header

C read data and write to text file
      ival = 1
      do irec=1,width*height/4
        read(1,rec=irec+hdrsiz/4) logrec
        do lbyte=1,4
          values(ival) = ichar(logrec(lbyte))
          ival = ival+1
        enddo
      enddo

      write(*,*) ival-1, ' values read'

      open(unit=2,file='199701.txt')

      write(2,*) values

      stop
      end


Scaled 2 byte data in two dimensional array

      program readaari

      parameter (width = 721, height = 721, scale = 0.01)

      real*4 lat(width,height), lon(width,height)
      integer*2 datum, column, row

      open(unit=1, file='aarilats.bin', form='unformatted',
     1     access='direct', recl=2)

      open(unit=2, file='aarilons.bin', form='unformatted',
     1     access='direct', recl=2)

C     note switch to column,row here

      do 200 row = 1, height
        do 100 column = 1, width
          read(1) datum
          lat(column,row) = datum*scale
          read(2) datum
          lon(column,row) = datum*scale
 100    continue
 200  continue

      close(1)
      close(2)


      write(*,*) 'column, row, lat, lon'
      write(*,*) 721, 721, lat(721,721), lon(721,721)
      write(*,*) 1, 1, lat(1,1), lon(1,1)
      write(*,*) 1, 721, lat(1,721), lon(1,721)
      write(*,*) 721, 1, lat(721,1), lon(721,1)
      write(*,*) 361, 361, lat(361,361), lon(361,361)

      end


Same data using FORM='SYSTEM'

      program readaari

      parameter (width = 721, height = 721, scale = 0.01)

      integer*2 lat(height, width), lon(height, width)

      open(unit=1, file='aarilats.bin', form='system')
      read(1) lat

      open(unit=2, file='aarilons.bin', form='system')
      read(2) lon

      close(1)
      close(2)

      write(*,*) 'row, column, lat, lon'
      write(*,*) 721, 721, scale*lat(721,721), scale*lon(721,721)
      write(*,*) 1, 1, scale*lat(1,1), scale*lon(1,1)
      write(*,*) 1, 721, scale*lat(1,721), scale*lon(1,721)
      write(*,*) 721, 1, scale*lat(721,1), scale*lon(721,1)
      write(*,*) 361, 361, scale*lat(361,361), scale*lon(361,361)

      end