Reputation: 383
I made a type in fortran called waveform and I made a function that reads through a file and makes an array of waveform types.
I want the function to return my waveform array, but I can't seem to figure out how to return the final array.
Below is my function
!@param none
!@return array of the configuration file
function getWilloughby()
integer::it !iterator for do loop
integer::size = 56661 !Expected dimension of waveforms array
integer::io
real::x
real::y
real::lat
real::windspeed !Wind speed at that point
real::a !Willougby A parameter
real::startX
real::startY
real::data(600)
type(waveform), allocatable ::waveforms(:) !An array to hold all waveforms
type(waveform), allocatable ::waveformsOut(:) !An array to hold all waveforms
allocate(waveforms(size))
allocate(waveformsOut(size))
open(1, file="Willoughby56_530-w.txt")
!Load configuration parameters
read(1,*) windSteps, windStart, windInc, xSteps, ySteps, yInc, xInc, lat, xStart, yStart
write(*,*) "The wind parameters are" ,windStart, windSteps, windInc
write(*,*) "Loading Model Waveform data"
it = 1 !Iterator for do loop
do
it = it + 1
read(1,*, IOStat = io) x, y, windspeed, lat, a , startX, startY, data
if(io > 0) then
write(*,*) 'Check input. There was an error parsing the file'
write(*,*) 'The error number is:' ,io
exit
else if (io < 0) then
write(*,*) 'Finished reading the Willoughby configuration file'
exit
else
!Read the Willoughby config file
!Load each model waveform into a waveform type
!Place the waveform into the waveforms array
!write(*,*) "Waveform:", x, y, windspeed, lat, a, startX, startY, data
waveforms(it)%x = x
waveforms(it)%y = y
waveforms(it)%windspeed = windspeed
waveforms(it)%lat = lat
waveforms(it)%startX = startX
waveforms(it)%startY = startY
waveforms(it)%data = data
!write(*,*) waveforms(it)%data
end if
end do
write(*,*) waveforms
end function getWilloughby
When I want to call the function from my main program like so, I get an error:
type(waveform), allocatable::modelwaveforms(:)
modelwaveforms = getWilloughby()
Gfortran Compier error:
modelwaveforms = getWilloughby()
1
Error: Can't convert REAL(4) to TYPE(waveform) at (1)
If I change the modelwaveforms(:) array into a REAL array like the error says, it compiles, but only gives me one value from the array.
What am I doing wrong?
Upvotes: 1
Views: 134
Reputation: 6915
You are not defining a type for getWilloughby so it is defaulting to real under implicit typing (use implicit none
to turn off implicit typing so you'll get an error about not defining the return type).
To fix this you will need to define the return type, e.g.:
type(waveform), allocatable, dimension(:) :: getWilloughby ! return value
If you have a different variable that you want to be returned by the function, you can define the function as:
function getWilloughby() result(someOtherVariableName)
and make sure that someOtherVariableName
is of type type(waveform)
and if the function is not in a module, you will need to define an explicit interface in the Fortran file that calls the function, e.g.
interface
function getWilloughby()
type(waveform), allocatable, dimension(:) :: getWilloughby
end function
end interface
Upvotes: 2