打印/写入嵌套的派生数据类型

时间:2016-05-25 03:14:43

标签: fortran

我从一位同事那里获得了以下模块。我正在尝试创建一个程序,返回派生数据类型中的数据。我没有衍生数据类型的经验。我尝试了程序TABLE(文件的结尾),但我无法得到任何东西。如何打印或编写派生数据类型?

  C=======================================================================
  MODULE mod_AsosCommDates
  C=======================================================================

  IMPLICIT NONE

  INTEGER, PARAMETER :: NCommDates=2

  TYPE ASOSData
        CHARACTER (LEN=40) :: City
        CHARACTER (Len=60) :: Location
        CHARACTER (LEN=2)  :: State
        INTEGER            :: IWBAN
        CHARACTER (LEN=4)  :: INTCALL
        CHARACTER (LEN=3)  :: USACALL
        REAL               :: DDLAT
        REAL               :: DDLON
        CHARACTER (LEN=8)  :: COMMISDATE
        CHARACTER (LEN=3)  :: PUB
        CHARACTER (LEN=3)  :: SITETYPE
        REAL               :: ANEM_FEET
        REAL               :: ANEM_METERS
  END TYPE ASOSData

  TYPE (ASOSDATA), DIMENSION(NCommDates) :: ASOSCommDates

  CONTAINS

     SUBROUTINE InitAsosCommInfo (ASOSCommDates)

     TYPE (ASOSDATA), DIMENSION(NCommDates) :: ASOSCommDates

  ASOSCommDates(1) = ASOSDATA("ANCHORAGE","LAKE HOOD SEAPLANE BASE",
 &  "AK",26491,"PALH","LHD",61.17,-149.96,"19980122","NO","FAA",
 &  33.00,10.05)
  ASOSCommDates(2) = ASOSDATA("ANCHORAGE","MERRILL FIELD AIRPORT",
 &  "AK",26409,"PAMR","MRI",61.21,-149.85,"19971015","NO","FAA",
 &  16.00,4.87)

         END SUBROUTINE InitAsosCommInfo

  END MODULE mod_AsosCommDates

  PROGRAM TABLE
  USE mod_AsosCommDates
  CALL InitAsosCommInfo(ASOSCommDates)
  PRINT *,ASOSCommDates(1)%ASOSData%City
  END PROGRAM TABLE

发布时间:2016年5月25日美国东部时间上午10:54 最初发布的示例与此帖子的问题相关的错误有不同的错误。

NCommDates已从NCommDates=886更改为NCommDates=2 CALL行已从CALL InitAsosCommInfo更改为CALL InitAsosCommInfo(ASOSCommDates)

编译时出错:

$ gfortran 2.for
2.for:48.39:

      PRINT *,ASOSCommDates(1)%ASOSData%City
                    1 Error: 'asosdata' at (1) is not a member of the 'asosdata' structure

1 个答案:

答案 0 :(得分:2)

尽管Fortran标准规定您可以为I / O编写类型绑定过程,但许多编译器还不支持此操作。因此,最简单的解决方案是按组件方式打印派生类型。

但是,在您的代码中,访问组件时无需指定类型:

PRINT *,ASOSCommDates(1)%City

......和其他组件类似。