你如何在Fortran中返回一个类型化的数组?

时间:2014-07-30 23:49:16

标签: arrays types fortran fortran90 gfortran

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

我希望函数返回我的波形数组,但我似乎无法弄清楚如何返回最终数组。

以下是我的功能

 !@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

当我想从我的主程序中调用该函数时,我收到错误:

type(waveform), allocatable::modelwaveforms(:) 
modelwaveforms = getWilloughby()

Gfortran Compier错误:

  modelwaveforms = getWilloughby()
               1
  Error: Can't convert REAL(4) to TYPE(waveform) at (1)

如果我将modelwaveforms(:)数组更改为REAL数组(如错误所示),它会编译,但只能从数组中给出一个值。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您没有为getWilloughby定义类型,因此它在隐式类型下默认为默认(使用implicit none关闭隐式类型,因此您将收到有关未定义返回类型的错误)。

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

type(waveform), allocatable, dimension(:) :: getWilloughby ! return value 

如果您希望函数返回另一个变量,则可以将函数定义为:

function getWilloughby() result(someOtherVariableName)

并确保someOtherVariableName的类型为type(waveform)

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

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