Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from unknown number of columns

Status
Not open for further replies.

cleggy84

Programmer
Feb 24, 2006
2
GB
We have a model that outputs acid concentration data on a 2-dimensional grid in the form,

X,Y,HCl,HF,H2SO4,.... (Column headers)
1,1,0.4,0.8,0.8,.... (Data)
2,1,0.2,0.8,0.9,....
ETC.

We need to read HF data from this file. Unfortunately, depending upon the initial model setup, the number of acids modelled and column location of HF varies. Is there any easy way that we can find out which column HF is in and then read data from this column? I have not used Fortran since leaving university so am a little rusty I'm afraid.

Thanks in advance




 
Try this - not exactly elegant but it does the job. The problem is comma separated data. It is a pain in most languages.
Code:
      program findhf
      integer istream
      parameter (istream = 10)
      integer iCol
      character*128 cLine
      character*8   cSearch, cTag
      real vSearch
      cSearch = 'HF'
      open (istream, file='findhf.dat', status='old')
!
!     Get the header
      read (istream, '(A)') cLine
      iEnd = 1
      iStart = 1
      iSearchCol = 0
      do while (cLine(iEnd:iEnd) .ne. ' ')
         if (cLine(iEnd:iEnd) .eq. ',') then
!
!           Found a comma - is it the one we want
            iSearchCol = iSearchCol + 1
            cTag = cLine(iStart:iEnd - 1)
            if (cTag .eq. cSearch) then
               print *, cSearch, ' found at column ', iSearchCol
               goto 50
            end if
!
!           Set the start of the next one
            iStart = iEnd + 1
         end if
         iEnd = iEnd + 1
      enddo
!
!     Get the data
50    continue
         read (istream, '(A)', end = 100) cLine
         iCol = 0
         iStart = 1
         iEnd = 1
         do while (cLine(iEnd:iEnd) .ne. ' ')
            if (cLine(iEnd:iEnd) .eq. ',') then
!
!              Found a comma - extract the number
               iCol = iCol + 1
               if (iCol .eq. iSearchCol) then
                  read (cLine(iStart:iEnd - 1), '(G5.1)') vSearch
                  print *, vSearch
                  goto 90
               end if
!
!              Set the start of the next one
               iStart = iEnd + 1
            end if
            iEnd = iEnd + 1
         enddo
90       continue
         goto 50
!
!     No more data
100   continue
      close (iStream)
      stop
      end
 
This is exactly what I was looking for and works perfectly. A great many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top