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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran Coding Problem

Status
Not open for further replies.

JDruryPhys

Programmer
Apr 1, 2013
3
AU
Hi all,
I have a problem with a Fortran script I am trying to use for a research project. I have several data sets and I wish to be able to process them using the one Fortran script. This is a script I have been provided with and my knowledge is very basic (I have had about 4 days to try to cram in as much knowledge about Fortran as possible). I have a file (qxlc.flags where the x is the dataset number e.g. q1lc.flags, q2lc.flags etc.) in the same directory for each data set as this script which has approximately 4000 lines with the last line of the file being the number of lines above it, format: "__4370_" where the '_'s are spaces (the exact number differs for each data set). What I wish to do is get the number of lines from the end of the qxlc.flags file and use it in the code. I have included some of the code below. Lines marked ##### Contain what I want to do and while I could enter these values manually, I have a lot of datasets and so would prefer to enter them automatically if possible. Any help would be much appreciated.
J.Drury

Code:
program savecl
c  this set up for dataset 3
c  *** NOTE:  entries 'nv' and 'ndat' need updating for each new dataset. Comments underneath corresponding lines.
      character*10 qxlcnme
c  name of file for dataset x
      integer nlines
c  Line number for position of last line in file
      parameter(nv=52481520,nap=20)
c  nv is a value found by: ls -l *.fits | awk '{if(NR==1) print $5/4}' in the same directory as the script. #####
      parameter(ntot=40000,nsng=2000)
c  constants for code.
      parameter(ndat=4370)
c  Number of lines in qxlc.flags file. I wish to change this to ndat = nlines somehow. #####
      parameter(nbuf0=9204,nbufg=10007)
c  start and general step skips
      dimension vectin(nv),vectall(ndat*nsng,nap),d1(ntot)
c  vectall will have saved only the active data sections
      dimension ifl(ndat)
      character*3 istat
      character*10 cflags
      character*41 fdata
      character*9 chd1
      character*9 filset
      data exps/1625.4/
c  ##### My current attempt to get the values I want, failed as I can't set variable values before I have found the value and apparently can't assign them afterwards either.
      open(unit=60,file='qxlc.txt')
      read(60,*) qxlcnme
      close(60)
      open(unit=66,file=qxlcnme)
      do
        read(66,*,END=70)
        nlines=nlines+1
      enddo
   70 close(66)
      ndat = nlines
##### End of attempt... Code continues.
c  open deck file
      open(unit=5,file='savecl.deck')
      read(5,20) filset,cflags,istat,chd1
      write(6,20) filset,cflags,istat,chd1
   20 format(a9,1x,a10,1x,a3,1x,a9)
      close(unit=5)
.----------------------------------------------
. There is more code here but I don't think its needed for this question. If extra detail is needed, please let me know.
.----------------------------------------------
      end
 
What I read from your code is, that you take the first line of qxlc.txt to decide on the name of the file to open. This looks queer. If you want to process all the files listed in qxlc.txt - assuming there is more than one - you should build a loop around this as well.

The part where you read all the lines of your file 'qxlcnme' counting them looks fine. But prior to couinting you should set nlines to zero. So I would propose

Code:
open (unit = 60, file = 'qxlc.txt')
rewind (60)
do 
    read (60, *, iostat = iEnd_60) qxlcnme
    if (iEnd_60 .ne. 0) exit
    open(unit=66,file=qxlcnme)
    rewind (66)
    nlines = 0
    do while (iend_66 .eq. 0)
        read(66,*,iostat = iEnd_66)
        nlines=nlines+1
    enddo
    close (66)
    ndat = nlines - 1        ! the loop above would count one line too much
enddo
close (60)

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Hi Norbert,
Thanks for your reply. The .txt file only has one filename in it in this case, thus the lack of the loop. The part I am having trouble with, is setting the parameter ndat equal to the count of nlines-1 (cheers for the correction). I realise this means changing ndat to another data type. The problem I struck when trying to do this is that there are arrays; dimension vectall(ndat) and dimension ifl(ndat), which use this value to create arrays with that many elements (if I understand it correctly). Is there a way to make this work
Cheers,
JDrury

PS I am compiling and running this fortran script from a bash script as one of many steps, so if it is possible to simply input the value of ndat (and the parameter nv) as arguments that would suit me as well.
 
Hi Norbert,

Thanks for the help but I have found a solution to this problem using sed to edit the .f file for each directory to replace these values.

Cheers,
J.Drury
 
If you have found a solution, that's fine.
Just for information: You could make these arrays allocatable:

Code:
subroutine sub (iErr)
real, allocatable, dimension(:) :: vectall
integer, allocatable, dimension(:) :: ifl

...
...

iErr = 0
open (unit = 60, file = 'qxlc.txt')
read (60, *) qxlcnme
close (unit = 60)
open (unit = 66, file = qxlcnme)
rewind 66
nlines = 0
do
    read (66, *, iostat = iEnd)
    if (iend .ne. 0) exit
    nLines = nLines + 1    ! count only if record was read!
enddo
allocate (vectall (nlines), stat = ierr)
allocate (ifl (nLines), stat = ierr)
if (ierr .ne. 0) then
    write (*,*) 'not enough memory'
    return
endif
rewind (66)
do j = 1, nLines
    read (66,*) vectall (j), ifl (j)
enddo
close (unit = 66)

! process data

deallocate (vectall)
deallocate (ifl)

return
end

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top