Re: Allocatable Arrays As Outputs
- From: bonzaboy@xxxxxxxxx
- Date: Wed, 30 Jan 2008 09:34:22 -0800 (PST)
On Jan 30, 3:47 pm, Infinity77 <andrea.gav...@xxxxxxxxx> wrote:
Hi All,
first of all, please forgive my poor mastering of Fortran. I am
still a beginner who uses Fortran sporadically.
I have a subroutine in which I declare 4 output variables as
allocatable. This is because, a priori, I don't know their lengths
(they're all one-dimensional vectors), and I discover this information
only when I read a file which contains both their lengths and the data
used to fill these vectors.
My problem is, I don't know how to correctly "declare" these variables
in the main program, as I keep getting errors on assumed-shape/size
and dummy arguments. My knowledge of Fortran is so slim I can't find a
way to solve this problem.
I am attaching a simplified version of my small program (1 subroutine
+ the main) in which I have only 1 allocatable array. I have used my
google-fu but either I can't interpret correctly the problem or what I
am doing is completely wrong.
Thank you for all your suggestions.
Andrea.
subroutine ReadINSPEC(fileName, propertyNames)
use ReadECLIPSEBinary
implicit none
character (len=*), intent(in) :: filename
character (len=8), allocatable, intent(out) :: propertyNames(:)
integer numberOfProps
character (len=4) keywordType
character (len=8) keywordName
logical feof
open(unit=1, file=filename, form='UNFORMATTED',
convert='BIG_ENDIAN')
feof = .false.
while_loop: do while (.not.feof)
read(1, end=20, err=18) keywordName, numberOfProps,
keywordType
if (keywordName == 'NAME') then
allocate(propertyNames(numberOfProps))
call ReadECLIPSEData(1, numberOfProps,
stringVector=propertyNames)
exit while_loop
endif
18 continue
enddo while_loop
20 continue
deallocate(propertyNames)
close(1)
return
end subroutine ReadINSPEC
program main
! ???????
! I don't know if this is possible or not...
! ???????
character(len=14) :: fileName
character(len=8) :: propertyNames(:)
fileName = 'OPT_INJ.INSPEC'
call ReadINSPEC(fileName, propertyNames)
end program main
Hi,
You can try using pointers instead (they are pretty simple to use in
f95). If you declare propertynames as a pointer, I.e. replace
'allocatable' with 'pointer', you can still allocate memory as you
have. Then you simply need to declare the variable in the calling
program to also be a pointer (same shape). You also have to use the
save attribute in the subroutine to ensure the memory isnt lost when
the subroutine exits.
If you want to call this routine more than once using this approach
you'll want to assign new memory in the main program (i.e allocate a
new variable), and add something to the start of the subroutine to
free up the memory.
Others may know a better way
Hope this is useful
.
- Follow-Ups:
- Re: Allocatable Arrays As Outputs
- From: Richard Maine
- Re: Allocatable Arrays As Outputs
- From: glen herrmannsfeldt
- Re: Allocatable Arrays As Outputs
- References:
- Allocatable Arrays As Outputs
- From: Infinity77
- Allocatable Arrays As Outputs
- Prev by Date: The concept of a record
- Next by Date: Re: The concept of a record
- Previous by thread: Re: Allocatable Arrays As Outputs
- Next by thread: Re: Allocatable Arrays As Outputs
- Index(es):
Relevant Pages
|
|