c=========================================================== c Returns a double precision vector (one-dimensional c array) read from file 'fname'. If 'fname' is the c string '-', the vector is read from standard input. c c The file should contain one number per line; invalid c input is ignored. c c This routine illustrates a general technique for c reading data from a FORMATTED (ASCII) file. In c Fortran, one associates a "logical unit number" c (an integer) with a file via the OPEN statement. c The unit number can then be used as the first c "argument" of the READ and WRITE statements to c perform input and output on the file. c c Fortran reserves the following unit numbers: c c 5 terminal input (stdin) c 6 terminal output (stdout) c 0 error output on Unix systems (stderr) c=========================================================== subroutine dvfrom(fname,v,n,maxn) c----------------------------------------------------------- c Arguments: c c fname: (I) File name c v: (O) Return vector c n: (O) Length of v (# read) c maxn: (I) Maximum number to read c----------------------------------------------------------- implicit none c----------------------------------------------------------- c The integer functions 'indlnb' and 'getu' are c defined in the 'p410f' library. c----------------------------------------------------------- integer indlnb, getu c----------------------------------------------------------- c Declaration of routine arguments: note c "adjustable dimensioning" of v; any array which c is declared with adjustable dimensions must be c a subroutine argument; any adjustable dimensions c must also be subroutine arguments. c----------------------------------------------------------- character*(*) fname integer n, maxn real*8 v(maxn) c----------------------------------------------------------- c Programming style: Use parameter (ustdin) rather c than constant value (5) for stdin logical unit # c----------------------------------------------------------- integer ustdin parameter ( ustdin = 5 ) c----------------------------------------------------------- c Local variables: c c vn: Current number read from input c ufrom: Logical unit number for READ c rc: For storing return status from READ c----------------------------------------------------------- real*8 vn integer ufrom, rc c----------------------------------------------------------- c Intialize c----------------------------------------------------------- n = 0 c----------------------------------------------------------- c Read from stdin? c----------------------------------------------------------- if( fname .eq. '-' ) then c----------------------------------------------------------- c Set unit number to stdin default c----------------------------------------------------------- ufrom = ustdin else c----------------------------------------------------------- c Get an available unit number c----------------------------------------------------------- ufrom = getu() c----------------------------------------------------------- c Open the file for formatted I/O c----------------------------------------------------------- open(ufrom,file=fname(1:indlnb(fname)), & form='formatted',status='old',iostat=rc) if( rc .ne. 0 ) then c----------------------------------------------------------- c Couldn't open the file, print error message c and return. c----------------------------------------------------------- write(0,*) 'dvfrom: Error opening ', & fname(1:indlnb(fname)) return end if end if c----------------------------------------------------------- c Input numbers into vector (one per line) until c EOF or maximum allowable number read c----------------------------------------------------------- 100 continue read(ufrom,*,iostat=rc,end=200) vn if( rc .eq. 0 ) then n = n + 1 if( n .gt. maxn ) then write(0,*) 'dvfrom: Read maximum of ', & maxn, ' from ', & fname(1:indlnb(fname)) n = maxn go to 200 end if v(n) = vn end if go to 100 200 continue c----------------------------------------------------------- c If we are reading from a file, close the file. c This releases the unit number for subsequent use. c----------------------------------------------------------- if( ufrom .ne. ustdin ) then close(ufrom) end if return end