UCDavisPrgmer
Technical User
-just can't seem to do it.
My goal: make a simple subroutine that reads in a text file with 1 column of numbers and outputs an array with those numbers.
I can do it no problem in the main program but have been going nuts trying to output an array for which I don't know the dimensions in advance. I know you can use the allocatable inside a procedure, but I haven't seen it used when the array is an output of the procedure.
I've tried assumed shape and assumed size arrays but have had no luck.
The code I would like to put into a subroutine (that works in the main) is found below.
thanks for any help,
-J
*******************************************************
!_____________________________________________________
! Declaration of Variables
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
! Arrays:
REAL, Allocatable, dimension) :: FlowArray
!______________________________________________________
! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', &
POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
GO TO 999
END IF fileopen
! Open was succesful -> count data length to size array
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
! Read input file into an array
ALLOCATE ( FlowArray(nflows),STAT=status) ! allocate memory
REWIND (Unit=20) ! rewind file, go back to beginning
READ (20,*) FlowArray ! read in data and place into array
My goal: make a simple subroutine that reads in a text file with 1 column of numbers and outputs an array with those numbers.
I can do it no problem in the main program but have been going nuts trying to output an array for which I don't know the dimensions in advance. I know you can use the allocatable inside a procedure, but I haven't seen it used when the array is an output of the procedure.
I've tried assumed shape and assumed size arrays but have had no luck.
The code I would like to put into a subroutine (that works in the main) is found below.
thanks for any help,
-J
*******************************************************
!_____________________________________________________
! Declaration of Variables
CHARACTER(len=20):: InputFile = 'keran.dat'
INTEGER::status ! status = 0 for success
INTEGER::nflows ! No. of flow data values
REAL:: temp ! temp var to read in data
! Arrays:
REAL, Allocatable, dimension) :: FlowArray
!______________________________________________________
! Open input file:
OPEN (unit=20, File=InputFile, STATUS='OLD', ACTION='READ', &
POSITION='REWIND',IOSTAT=status)
! Check if file is open
fileopen: If (status /= 0) THEN
WRITE (*,*) 'Hey, I failed to open the file'
GO TO 999
END IF fileopen
! Open was succesful -> count data length to size array
DO
READ(20, *,IOSTAT=status) temp
IF (status /= 0) EXIT
nflows = nflows + 1
END DO
! Read input file into an array
ALLOCATE ( FlowArray(nflows),STAT=status) ! allocate memory
REWIND (Unit=20) ! rewind file, go back to beginning
READ (20,*) FlowArray ! read in data and place into array