Fortran Coder

查看: 10216|回复: 9
打印 上一主题 下一主题

[数学库] linux下使用netcdf代码的错误

[复制链接]

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
跳转到指定楼层
楼主
发表于 2017-4-16 20:46:20 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
调用子程序完成文件的输入和输出,程序如下(应用求助过的程序-linux下):
[Fortran] 纯文本查看 复制代码
program link

      implicit none
      external wheat_area,allockey_lei,integrated_wheat,link_area_calendar

      integer k,i,j
      integer,parameter::nCol
      character(90) str(nCol)
      character(90) str1(nCol1)
      integer,dimension(3166782)::ALLOC_KEY1
      integer,dimension(792304)::alloc_key,m_1,n_1
      real,dimension(3166782)::X,Y
      real,dimension(792304)::lon_1,lat_1,whea,plant_1,harvest_1
      character*15,dimension(3166782)::Name
      character*15,dimension(792304)::name_1
      real,dimension(792304)::lon_1,lat_1,
      character*15,dimension(792304)::name_1
      integer,parameter::latitude=360,longitude=720
      real lon_2(longitude)
      real lat_2(latitude)
      real plant_2(longtitude,latitude)
      real harvest_2(longtitude,latitude)

      call wheat_area(str,nCol)
      call allockey_lei(str1,nCol1)
      call integrated(ALLOC_KEY1,alloc_key,X,Y,lon_1,lat_1,whea,Name,name_1)
      call linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,whea,plant_1,plant_2,harvest_1,harvest_2,name_1)

      end program

      !number 1,extract area
      subroutine wheat_area(nCol,str)
         implicit none

         integer k,i
         integer,parameter::nCol=113     !Define a constant nCol equal to 113
         character(90) str(nCol)
         character(9),parameter:: strHead(6)=['alloc_key','whea     ','whea_h   ','whea_l   ', 'whea_i   ','whea_s   ']
         logical (kind=4) L(nCol)
         open(10,file="/home/iga_qtong/fortran_xcao/gae_c/spam_p.csv")
         open(11,file="/home/iga_qtong/fortran_xcao/gae_c/whout1.csv")
         read(10,*) str  !read the head of table
         L(i)= .false.       !determin the column to read
         do i=1,nCol
            str(i)=AdjustL(str(i))
            if(any(Trim(str(i))==strHead))then    !if the string equal to the strHead
            L(i)= .true.
            write(11,"(a,',')",advance='no') Trim(str(i))
            end if
         end do
         write(11,*)
         do
           read(10,*,iostat=k) str
           if(k/=0) exit
           do i=1,nCol
           if(.NOT.L(i)) cycle
           write(11,'(a,",")',advance='no') Trim(str(i))
           end do
         write(11,*)
         end do

      end subroutine wheat_area

      !number 2,extract longitude and latitude
      subroutine allockey_lei(nCol1,str1)
         implicit none

         integer k,i
         integer,parameter::nCol1=6     !Define a constant nCol equal to 6
         character(90) str1(nCol1)
         character(9),parameter::strHead(4)=['X        ','Y        ','Name     ','ALLOC_KEY']
         logical (kind=4) L(nCol1)

         open(12,file="/home/iga_qtong/fortran_xcao/gae_c/allockey.csv")
         open(13,file="/home/iga_qtong/fortran_xcao/gae_c/al_new1.csv")
         read(12,*) str1  !read the head of table
         L= .false.       !determin the column to read
         do i=1,nCol1
            str1(i)=AdjustL(str1(i))
         if(any(Trim(str1(i))==strHead))then        !if the string equal to the strHead
            L(i)= .true.
            write(12,"(a,',')",advance='no') Trim(str1(i))
            end if
         end do

         do
            read(10,*,iostat=k) str1
            if(k/=0) exit
            do i=1,nCol1
            if(.NOT.L(i)) cycle
            write(13,'(a,",")',advance='no') Trim(str1(i))
            end do

         end do

      end subroutine allockey_lei

      !number 3
      subroutine integrated(ALLOC_KEY1,alloc_key,X,Y,lon_1,lat_1,whea,Name,name_1)
         implicit none
         integer i,j
         integer,dimension(3166782)::ALLOC_KEY1   !extract variable from al_new.csv
         integer,dimension(792304)::alloc_key     !extract variable from whout.csv
         real,dimension(3166782)::X,Y             !X and Y got from al_new.csv
         real,dimension(792304)::whea             !wheat area in whout.csv
         real,dimension(792304)::lon_1,lat_1      !selected longtitude and latitude to the new file
         character*15,dimension(3166782)::Name    !country name
         character*15,dimension(792304)::name_1   !selected country name
         character c                              !c for null read
         open(14,file="/home/iga_qtong/fortran_xcao/gae_c/al_new1.csv")
         open(15,file="/home/iga_qtong/fortran_xcao/gae_c/whout1.csv")
         open(16,file="/home/iga_qtong/fortran_xcao/gae_c/integrated_wheat1.csv")
         !read al_new.csv
         read(14,*) c
         do i=1,3166782
            read(14,*) X(i),Y(i),Name(i),ALLOC_KEY1(i)
         end do
         write(*,*) X(899920),Y(899920),ALLOC_KEY(899920)
         !read whout.csv
         read(15,*)c
         do j=1,792304
            read(15,*) alloc_key(j),whea(j)
         end do
         write(*,*) 139000,alloc_key(139000),whea(139000)
         !tegrated wheat_area and allockey
         do j=1,792304
            do i=1,3166782
             if(alloc_key(j)==ALLOC_KEY1(i)) then
               name_1(j)=Name(i)
               lon_1(j)=X(i)
               lat_1(j)=Y(i)
              write(16,10) alloc_key(j),',',name_1(j),',',lon_1(j),',',lat_1(j),',',whea(j)
           10 format(I8,A15,F10.3,F10.3,F10.2)
              end if
             end do
         end do

         close(14)
         close(15)
         close(16)

      end subroutine integrated


      !number 4, to link calendar and area by lon and lat
      subroutine linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,whea,plant_1,
         &plant_2,harvest_1,harvest_2,name_1)
         implicit none
         integer i,j

         integer,dimension(792304)::alloc_key,m_1,n_1
         real,dimension(792304)::lon_1,lat_1,whea,plant_1,harvest_1
         character*15,dimension(792304)::name_1
         integer,parameter::latitude=360,longitude=720   !calendar
         integer ierr,ncid,varid,len_file
         real lon_2(longitude)
         real lat_2(latitude)
         real plant_2(longtitude,latitude)
         real harvest_2(longtitude,latitude)
         character*299 calendar_file

         include 'netcdf.inc'

         calendar_file="/home/iga_qtong/fortran_xcao/gae_c/Wheat.nc"
         plant_1=0.1
         harvest_1=0.1
         m_1=0
         n_1=0

         open(17,file="/home/iga_qtong/fortran_xcao/gae_c/integrated_wheat1.csv")
         open(18,file="/home/iga_qtong/fortran_xcao/gae_c/link_area_calendar1.csv")

         !open the integrated_wheat1.csv and extract information
         do i=1,792304
            read(17,*) alloc_key(i),name_1(i),lon_1(i),lat_1(i),whea(i)
         end do
         write(*,*) 1,alloc_key(1),name_1(1),lon_1(1),lat_1(1),whea(1)
         write(*,*) i-1,alloc_key(i-1),name_1(i-1),lon_1(i-1),lat_1(i-1),whea(i-1)

         !open the Wheat.nc and extract information
         len_file=len_trim(calendar_file)
         ierr=nf_open(trim(calendar_file),nf_nowrite, ncid)

         ierr=nf_inq_varid(ncid, 'plant', varid)     !get "plant"
         ierr=nf_get_var_real(ncid, varid, plant_2)
         write(*,*) "the status of plant is",ierr
         write(*,*) plant_2(50,70)


         ierr=nf_inq_varid(ncid, 'harvest', varid)   !get "harvest"
         ierr=nf_get_var_real(ncid, varid, harvest)
         write(*,*) "the status of harvest is",ierr
         write(*,*) harvest_2(50,70)


         ierr=nf_inq_varid(ncid, 'longitude', varid)   !get "longitude"
         ierr=nf_get_var_real(ncid, varid, lon_2)
         write(*,*) "the status of longitude is",ierr
         write(*,*) lon_2(50)


         ierr=nf_inq_varid(ncid, 'latitude', varid)   !get "latitude"
         ierr=nf_get_var_real(ncid, varid, lat_2)
         write(*,*) "the status of longitude is",ierr
         write(*,*) lat_2(50)

         ! match lon and lat by a loop program
         do i=1,793204             !longitude
            do j=1,720
              if(abs(lon_1(i)-lon_2(j))-mival(abs(lon_1(i)-lon_2(j))==0) then
                m_1(i)=j
                write(*,*) i,m_1(i)
              end if
            end do
         end do

         do i=1,793204              !latitude
            do j=1,360
              if(abs(lat_1(i)-lat_2(j))-mival(abs(lat_1(i)-lat_2(j))==0) then
                n_1(i)=j
                write(*,*) i,n_1(i)
              end if
            end do
         end do

         do i=1,792304              !assign plant_2 to plant_1
           plant_1(i)=plant_2(m_1(i),n_1(i))
           write(*,*) plant_1(i)

         do i=1,792304              !assign harvest_2 to harvest_1
           harvest_1(i)=harvest_2(m_1(i),n_1(i))
           write(*,*) harvest_1(i)
          end do

          ! Last, write varibales into a new file
          do i=1,793204
          write(18,100) alloc_key(i),',',name_1(i),',',lon_1(i),',',lat_1(i),',',whea(i),',',
          &plant_1(i),',',harvest_1(i),',',m_1(i),',',n_1(i)
        100 format(I8,A15,F10.3,F10.3,F10.2,F10.2,F7.1,I5,I5)
           write(*,*) 1,alloc_key(1),name_1(1),lon_1(1),lat_1(1),whea(1)
           write(*,*) i,alloc_key(i),name_1(i),lon_1(i),lat_1(i),whea(i)

           close(17)
           close(18)
        subroutine linkall


编译出现了问题,请问:
1、各个子程序之间是否可以使用同样的变量
2、一旦其中有读取nc文件,是否编译要按Nc文件的翻译
3、请问是否能帮忙指出程序里的错误。

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
沙发
 楼主| 发表于 2017-4-16 22:00:44 | 显示全部楼层
fcode 发表于 2017-4-16 21:06
1.可以
2.没有“按NC文件的翻译”这种说法
3.你遇到了什么错误?

错误如下:
link.f90(16): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: %FILL <IDENTIFIER>
      real,dimension(792304)::lon_1,lat_1,
------------------------------------------^
link.f90(36): error #6406: Conflicting attributes or multiple declaration of name.   [NCOL]
         integer,parameter::nCol=113     !Define a constant nCol equal to 113
----------------------------^
link.f90(70): error #6406: Conflicting attributes or multiple declaration of name.   [NCOL1]
         integer,parameter::nCol1=6     !Define a constant nCol equal to 6
----------------------------^
link.f90(147): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: * <IDENTIFIER>
      subroutine linkall(alloc_key,m_1,n_1,lon_1,lon_2,lat_1,lat_2,
-------------------------------------------------------------------^
link.f90(148): error #5082: Syntax error, found '&' when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
         &whea,plant_1,plant_2,harvest_1,harvest_2,name_1
----------^
link.f90(211): error #5082: Syntax error, found IDENTIFIER 'THEN' when expecting one of: ( * ) :: , <END-OF-STATEMENT> ; . % (/ + - [ : ] /) . ' ** / ...
              if(abs(lon_1(i)-lon_2(j))-mival(abs(lon_1(i)-lon_2(j))==0) then
-------------------------------------------------------------------------^
link.f90(220): error #5082: Syntax error, found IDENTIFIER 'THEN' when expecting one of: ( * ) :: , <END-OF-STATEMENT> ; . % (/ + - [ : ] /) . ' ** / ...
              if(abs(lat_1(i)-lat_2(j))-mival(abs(lat_1(i)-lat_2(j))==0) then
-------------------------------------------------------------------------^
link.f90(238): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> <INTEGER_CONSTANT> ...
            write(18,100) alloc_key(i),',',name_1(i),',',lon_1(i),',',lat_1(i),',',whea(i),',',
-----------------------------------------------------------------------------------------------^
link.f90(239): error #5082: Syntax error, found '&' when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
            &plant_1(i),',',harvest_1(i),',',m_1(i),',',n_1(i)
-------------^
link.f90(246): error #5082: Syntax error, found END-OF-FILE when expecting one of: <LABEL> <END-OF-STATEMENT> ; BLOCK PROGRAM BLOCKDATA MODULE INTEGER REAL COMPLEX ...
        subroutine linkall
--------------------------^
compilation aborted for link.f90 (code 1)

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
板凳
 楼主| 发表于 2017-4-17 14:28:59 | 显示全部楼层
fcode 发表于 2017-4-17 08:21
第7行
integer,parameter::nCol
常数定义时必须赋值,比如

运行时出现了以下问题:
forrtl: severe (24): end-of-file during read, unit 14, file /home/iga_qtong/fortran_xcao/gae_c/al_new1.csv
请问:
1、将第二个子程序单独运行的时候可以,但是在这里又说文件过长是为什么?如何改正?
2、该程序第三个子程序需要用到第一和第二个子程序的输出文件,可以直接Open吗?还是可以在子程序里call sub2?
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-8 08:35

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表