Arrays 如何在Fortran中返回类型化数组?

Arrays 如何在Fortran中返回类型化数组?,arrays,types,fortran,fortran90,gfortran,Arrays,Types,Fortran,Fortran90,Gfortran,我在fortran中创建了一个名为waveform的类型,并创建了一个函数,可以读取文件并生成一个波形类型数组 我希望函数返回我的波形数组,但我似乎不知道如何返回最终的数组 下面是我的功能 !@param none !@return array of the configuration file

我在fortran中创建了一个名为waveform的类型,并创建了一个函数,可以读取文件并生成一个波形类型数组

我希望函数返回我的波形数组,但我似乎不知道如何返回最终的数组

下面是我的功能

 !@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
Gfortran Compier错误:

  modelwaveforms = getWilloughby()
               1
  Error: Can't convert REAL(4) to TYPE(waveform) at (1)
如果我将modelwaveforms(:)数组更改为实际数组,如错误所示,它将编译,但只从数组中给出一个值


我做错了什么?

您没有为getWilloughby定义类型,因此它在隐式类型下默认为real(使用
隐式无
关闭隐式类型,这样您将得到一个关于不定义返回类型的错误)

要解决此问题,您需要定义退货类型,例如:

type(waveform), allocatable, dimension(:) :: getWilloughby ! return value 
如果希望函数返回不同的变量,可以将函数定义为:

function getWilloughby() result(someOtherVariableName)
并确保
someOtherVariableName
的类型为
类型(波形)

如果函数不在模块中,则需要在Fortran文件中定义调用函数的显式接口,例如

interface
  function getWilloughby()
    type(waveform), allocatable, dimension(:) :: getWilloughby
  end function
end interface

非常感谢你的帮助!!!所以我将我的函数声明更改为类型(波形)函数getWilloughby(),它可以编译,但我没有设置错误。我需要知道如何将其声明为类型(波形)数组吗?@EddieEMasseyIII请查看我上面的编辑。我已经将返回类型更改为数组(正如我应该开始的那样),并相应地编辑了界面。不要忘记在函数中分配结果,我在您的代码中找不到它。我找到了!我没有意识到函数的返回值与名称相同,并且可以在函数本身中声明函数的类型。找到一本书,然后我就可以根据你说的来修改代码了。谢谢@Eddieemasseii很高兴听到这个消息。还请注意,您可以使用
result()
将任何变量指定为返回变量,作为函数声明的一部分,它不必与函数同名。
interface
  function getWilloughby()
    type(waveform), allocatable, dimension(:) :: getWilloughby
  end function
end interface